Вопрос

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?

Это было полезно?

Решение

If I understand your intention correctly, you are trying to build a one-to-many relationship between RateCard and Country. In other words- a country will have only one RateCard, and a RateCard can belong to many countries. Assuming that's the case, you really don't need the RateCardCountry model (which will be useful if you wanted it to be a many-to-many relationship).

You will need to have:

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

And make sure you have county_id foreign key in the RateCard table.

and then:

class Country < ActiveRecord::Base
  has_one :rate_card
end

Also, it seems that right now you have:

class Country < Geography

I am not sure if you are subclassing from a Geography class, as you have not provided the rest of the code.

Hope that helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top