Mohamad El-Husseini
2012-May-04 14:00 UTC
Why does Rails only save the joiner record in one instance but not another?
Assuming we have User, Account, and Role models, and a User has_many :accounts, through: :roles and vice-versa, with the following snippet Rails automatically creates the Role (joiner) record, but with the snippet after Rails will not. I think I''m misunderstanding how has_many through works, so I would appreciate any insight into the subject. def new @account = Account.new(params[:account]) @user = @account.users.build end def new @account = Account.new(params[:account]) @account.save end Using this snippet Rails *does not* save the Role record: (If you replace current_user.accounts.build with current_user.accounts.create Rails will save the Role record) def new @account = current_user.accounts.build end def create @account = current_user.accounts.build(params[:account]) @account.save end Models: class User < ActiveRecord::Base has_many :roles has_many :accounts, through: :roles end class Account < ActiveRecord::Base has_many :roles has_many :users, through: :roles accepts_nested_attributes_for :users end class Role < ActiveRecord::Base belongs_to :users belongs_to :accounts end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Say5Bd1g7yUJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Apparently Analagous Threads
- How do I force link_to/form helpers to use the superclass name in the path instead subclass?
- cat 5 'joiner'? (polycom 500 problem)
- delete joiner entry when a habtm entry is removed
- New package joineR
- Problems Processing multiple form elements generated by javascript actions