Hello all, I''m having a strange issue with a model that belongs_to two other models. The one association is polymorphic and the other is normal. ===============================class Role < ActiveRecord::Base belongs_to :user belongs_to :permission, :polymorphic => true end class User < ActiveRecord::Base has_many :roles has_many :programs, :through => :roles end class Program < ActiveRecord::Base has_many :roles, :as => :permissions has_many :users, :through => :roles end =============================== But for some reason... ===============================@user = User.find(1) @user.roles << Role.create({:role => 100, :user_id => @user.id}) Rails.logger.info @user.roles.inspect =============================== ...returns nothing. I''m very confused here. My roles table has --------------------------------------------------- | id | user_id | permission_id | permission_type | --------------------------------------------------- | 1 | 1 | NULL | NULL | --------------------------------------------------- If anyone can help me out I''d greatly appreciate it. Environment: -------------- Ruby: REE Rails: 3.1.2 Passenger Thank you, Sparkmasterflex -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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.
I''m sorry to anyone who has spent any time on this at all. I hope since I found the problem so quickly after posting this no one has. The issue was that I was attempting to find all roles for the currently logged in user. While they are creating a new User. Basically @user was set in the controller as User.new and I needed to call current_user.roles instead. Thank you listening to me and sorry again -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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.
Peter Vandenabeele
2012-Jan-18 18:03 UTC
Re: Re: Polymorphic and standard association issue
On Wed, Jan 18, 2012 at 6:53 PM, Keith Raymond <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m sorry to anyone who has spent any time on this at all. I hope since > I found the problem so quickly after posting this no one has. >No prob. I was looking into it and was going to suggest this version: @user = User.find(1) new_role = @user.roles.create({:role => 100}) Just a little bit neater (no need for the Role.create and for the "dirty" pointer to the user_id) ... or even better (IMHO) class User < ActiveRecord::Base has_many :roles, :inverse_of :user ... @user = User.find(1) new_role = @user.roles.build({:role => 100 <http://user.id/>}) new_role. ... = ... do all you have to do in memory and finish it off with if user.save # OK else # handle the problem end Which will give you a nice transaction over all. So, if it only partially succeeds, you have an all or nothing to you database. (0.2ms) BEGIN SQL (0.5ms) INSERT INTO ... SQL (0.4ms) INSERT INTO ... (13.1ms) COMMIT With the inverse_of, also the belongs_to is automatically filled in. HTH, Peter -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. 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.
Reasonably Related Threads
- Why are double sided polymorphic relationships lacking in Rails?
- undefined method for Polymorphic association using Searchlogic
- [PATCH] Polymorphic belongs_to association with :conditions issues
- please help - complicated polymorphic association
- Polymorphic has_and_belongs_to_many association