Last active
August 29, 2015 13:56
-
-
Save zyphlar/9122854 to your computer and use it in GitHub Desktop.
Creating a many-to-many via params and accepts_nested_attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Business < ActiveRecord::Base | |
has_many :businesses_users, :primary_key=>:business_id | |
has_many :users, through: :businesses_users | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#NO: | |
params = {"business"=>{"user_id"=>"1", "name"=>"my business name"}} | |
#NO: | |
params = {"business"=>{"users"=>{"id"=>"1"}, "name"=>"my business name"}} | |
#NO: | |
params = {"business"=>{"businesses_users"=>{"user_id"=>"1", "business_id"=>"1"}, "name"=>"my business name"}} | |
#NO: | |
params = {"business"=>{"businesses_users_attributes"=>{"user_id"=>"1", "business_id"=>"1"}, "name"=>"my business name"}} | |
#YES: | |
params = {"business"=>{"businesses_users_attributes"=>{"0"=>{"user_id"=>"1", "business_id"=>"1"}}, "name"=>"my business name"}} | |
business = Business.new(params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment