Question

i need your help to link multiple records (here countries) with an other record (here policy) in same time into a form (new policy).

My models like :

class Retailer < ActiveRecord::Base
   has_many :orders
   has_many :policies, :dependent => :destroy
end

class Policy < ActiveRecord::Base
    has_many :countries
end

class Country < ActiveRecord::Base
    has_many :orders
    has_many :users
    has_one :platform
end

What i want is to link my new policy when i create this one in his form with multiple countries.

I want to use check boxes into this form to check what country i link (all countries stored in DB will be there).

I dont know if my association is appropriate for this context but i am bit lost how to do it.

Can someone help how to accomplish this and show me how my view should be ?

Thanks in advance.

Was it helpful?

Solution

What i want is to link my new policy when i create this one in his form with multiple countries.

You want to set a belongs_to :country in your policy model instead of has_many :countries

class Policy < ActiveRecord::Base
    belongs_to :country
end

and has_many :policies in your country model

class Country < ActiveRecord::Base
    has_many :orders
    has_many :users
    has_one :platform
    has_many :policies
end

I want to use check boxes into this form to check what country i link (all countries stored in DB will be there).

For this,you can use collection_check_boxes.By setting like above(belongs_to :country),you will get country_id which you will be using with collection_check_boxes to check/uncheck the multiple countries.

Hope it helps!

Update

Might i have just wrong with the associations.In your case,it is a has_and_belongs_to_many.

class Policy < ActiveRecord::Base

has_and_belongs_to :countries

end

class Country < ActiveRecord::Base

has_many :orders
has_many :users
has_one :platform
has_and_belongs_to_many :policies

end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top