Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Use of Super or how to get the calling object.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Niklas Nson  
View profile  
 More options May 5 2012, 6:39 pm
From: Niklas Nson <niklasn...@me.com>
Date: Sat, 5 May 2012 15:39:32 -0700 (PDT)
Local: Sat, May 5 2012 6:39 pm
Subject: 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 ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Walker  
View profile  
 More options May 5 2012, 7:02 pm
From: Jeremy Walker <jez.wal...@gmail.com>
Date: Sun, 6 May 2012 00:02:07 +0100
Local: Sat, May 5 2012 7:02 pm
Subject: Re: [Rails] Use of Super or how to get the calling object.

On 5 May 2012 23:39, Niklas Nson <niklasn...@me.com> 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Niklas Nson  
View profile  
 More options May 5 2012, 11:29 pm
From: Niklas Nson <niklasn...@me.com>
Date: Sat, 5 May 2012 20:29:22 -0700 (PDT)
Local: Sat, May 5 2012 11:29 pm
Subject: Re: [Rails] Use of Super or how to get the calling object.

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Walker  
View profile  
 More options May 6 2012, 5:47 am
From: Jeremy Walker <jez.wal...@gmail.com>
Date: Sun, 6 May 2012 10:47:11 +0100
Local: Sun, May 6 2012 5:47 am
Subject: Re: [Rails] Use of Super or how to get the calling object.

On 6 May 2012, at 04:29, Niklas Nson <niklasn...@me.com> 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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »