Use of Super or how to get the calling object.

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 ?

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

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!

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.