Class User has_many :settings, :class_name => "UserSetting", :foreign_key => "user_id" Class UserSetting belongs_to :user In UserSetting i have class << self def set(name, value) setting = [here i need the calling user object].settings.find_or_initialize_by_name(name) end end what i want is to be abel to make a call like: user = User.find(1) user.settings.set("test", "test") Is this possible or am i just way off ? -- 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/-/V8fTT3CSZkcJ. 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.
On 5 May 2012 23:39, Niklas Nson <niklasnson-BUHhN+a2lJ4@public.gmane.org> wrote:> Class User > has_many :settings, :class_name => "UserSetting", :foreign_key => "user_id" > > Class UserSetting > belongs_to :user > > In UserSetting i have > class << self > def set(name, value) > setting = [here i need the calling user > object].settings.find_or_initialize_by_name(name) > end > end >I think there are two sensible options. Either passing the user into a class set method (so UserSetting.set(user, name, value)) or better, put a method on user. e.g. class User has_many :settings, :class_name => "UserSetting", :foreign_key => "user_id" def set(name, value) setting = self.settings.find_or_initialize_by_name(name) #... setting.save end end Usage: user = User.find(1) user.set("test", "foobar") On a side note, "set" strikes me as a dangerous method name. I don''t think it''s reserved, but it''s not particularly descriptive, and might clash with other stuff in gems etc. Maybe update_setting() would be better? Finally, you could maybe do: Class UserSetting belongs_to :user def self.set(name, value) setting = self.where(name: name).first setting = self.build unless setting #... setting.save end end Usage: user = User.find(1) user.settings.set(name, value) However, I think that''s a horrible solution because it''s relying on the set method being called on an association rather than itself. However, it most closes resembles your initial code, so I''ve included it as a possible solution. You might be able to go further and make it more sensible, but I think a method on user is better. Does any of that help? Jeremy Walker http://www.ihid.co.uk> what i want is to be abel to make a call like: > user = User.find(1) > user.settings.set("test", "test") > > Is this possible or am i just way off ? > > -- > 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/-/V8fTT3CSZkcJ. > 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. >-- 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.
Thank you for a great answer, i wanted to move it from the User model - but i guess the smartest way is to keep it in there, also did the renaming to update_setting ... Again thank you! -- 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/-/i04zI6m4C7sJ. 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.
On 6 May 2012, at 04:29, Niklas Nson <niklasnson-BUHhN+a2lJ4@public.gmane.org> wrote:> Thank you for a great answer, i wanted to move it from the User model - but i guess the smartest way is to keep it in there, also did the renaming to update_setting ... > Again thank you!My pleasure! I understand not wanting the method on user (that model always gets so cluttered!!). However, I think it''s fair that the user has responsibility for how its settings are managed and therefore I wouldn''t worry about leaving it there. FYI, the way i manage it in an app of mine, is to have a user_settings table, with a column for each setting. Then I can just do "user.settings.receive_newsletter = true". That ties your settings to your schema though, which may be good or bad depending on your situation. Thanks for taking the time to say thank you! Have a good day.> -- > 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/-/i04zI6m4C7sJ. > 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@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.