Ruby on rails Rails:显示关联的模型验证

Ruby on rails Rails:显示关联的模型验证,ruby-on-rails,validates-associated,Ruby On Rails,Validates Associated,我有三个模型 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, :

我有三个模型

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
class RateCardtrue,:university=>true
拥有多个:等级卡国家/地区,:依赖=>:destroy
有很多:费率卡详细信息,:依赖=>:销毁
有多个国家/地区,:至=>:rate\u card\u国家/地区
结束
类别RateCardCountrytrue,:university=>true
验证:rate\u card\u id,:presence=>true
属于:费率卡
属于:国家
结束
国家级<地理
有一张:信用卡
有一个:等级卡国家
结束
在rate_cards_controller中,我想创建/更新rate_卡,以便一个国家应该有一张rate_卡。。 为此,我在RateCardCountry模型中添加了唯一性验证。 现在我想在创建/更新速率卡时显示速率卡控制器中的错误。。
需要帮助吗?

如果我正确理解您的意图,您正试图在RateCard和Country之间建立一对多的关系。换句话说,一个国家只有一张信用卡,而一张信用卡可以属于多个国家。 假设是这样的话,你真的不需要RateCardCountry模型(如果你想让它成为一种多对多的关系,它会很有用)

您需要:

class RateCard < ActiveRecord::Base
  validate :name, :presence => true, :uniqueness => true
  belongs_to :rate_card
end
class RateCardtrue,:university=>true
属于:费率卡
结束
并确保您在RateCard表中有county_id外键

然后:

class Country < ActiveRecord::Base
  has_one :rate_card
end
class Country
而且,现在你似乎有:

class Country < Geography
国家级
我不确定您是否是从地理类中进行子类化,因为您没有提供其余的代码

希望有帮助