2

I have got three models

class RateCard < ActiveRecord::Base
  validate :name, :presence => true, :uniqueness => true

  has_many :rate_card_countries, :dependent => :destroy
  has_many :rate_card_details, :dependent => :destroy
  has_many :countries, :through => :rate_card_countries

end

class RateCardCountry < ActiveRecord::Base

  validates :country_id, :presence => true, :uniqueness => true
  validates :rate_card_id, :presence => true
  belongs_to :rate_card
  belongs_to :country
end

class Country < Geography

  has_one :rate_card
  has_one :rate_card_country

end

In rate_cards_controller i want to create/update rate_cards such that one country should have one rate_card.. For that i have added uniqueness validation in RateCardCountry Model. And NOw i want to display the error in rate_card_controller while creating/updating rate_cards.. Needed help?

4

1 回答 1

0

如果我正确理解您的意图,您正在尝试在 RateCard 和 Country 之间建立一对多的关系。换句话说,一个国家只有一个 RateCard,而一个 RateCard 可以属于多个国家。假设是这种情况,您真的不需要 RateCardCountry 模型(如果您希望它是多对多关系,这将很有用)。

您将需要:

class RateCard < ActiveRecord::Base
  validate :name, :presence => true, :uniqueness => true
  belongs_to :rate_card
end

并确保 RateCard 表中有county_id 外键。

接着:

class Country < ActiveRecord::Base
  has_one :rate_card
end

此外,现在您似乎拥有:

class Country < Geography

我不确定您是否是 Geography 类的子类,因为您没有提供其余代码。

希望有帮助。

于 2013-07-03T04:23:06.463 回答