Hi, I'd like to know why I can't, or how can I, access attributes like that:

class User < ActiveRecord::Base
def name
@first_name + @last_name
end
end

first_name and last_name are user attributes in the database.

Thanks in advance.

--
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@googlegroups.com.
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.

Search Discussions

  • Benjamin Iandavid Rodriguez at Jan 30, 2012 at 8:19 pm
    Have this instance variables been set anywhere in your app?

    If not, you wont be able to access attributes anywhere.

    I suggest to first find a record in the DB and set the results to and
    instance variable like:

    @var = User.find(params[:some_params])

    and then you will be able to access attributes for that object

    @var.name + @var.lastname

    2012/1/30 Rodrigo Ruiz <rodrigo.ruiz7@gmail.com>
    Hi, I'd like to know why I can't, or how can I, access attributes like
    that:

    class User < ActiveRecord::Base
    def name
    @first_name + @last_name
    end
    end

    first_name and last_name are user attributes in the database.

    Thanks in advance.

    --
    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@googlegroups.com.
    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@googlegroups.com.
    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.
  • Peter Vandenabeele at Jan 30, 2012 at 8:26 pm

    On Mon, Jan 30, 2012 at 9:09 PM, Rodrigo Ruiz wrote:

    Hi, I'd like to know why I can't, or how can I, access attributes like
    that:

    class User < ActiveRecord::Base
    def name
    @first_name + @last_name
    end
    end

    first_name and last_name are user attributes in the database.
    ActiveRecord dynamically defines "accessor methods" on the
    user instance.

    That user instance is accessible as 'self' inside the user instance,
    so you could do

    class User < ActiveRecord::Base
    def name
    self.first_name + self.last_name
    end
    end

    But, in Ruby, when using a _read_ accessor, you could also write it
    without the explicit 'self'.

    class User < ActiveRecord::Base
    def name
    first_name + last_name
    # same as self.first_name + self.last_name
    # if there is no first_name, last_name local variable
    # defined in the scope
    end
    end

    For _write_ accessors, you _must_ put the self in front, that is:

    self.full_name = "#{first_name} #{last_name}"

    And the @first_name you asked about, that is an "instance variable".

    Read one of the many Ruby tutorials or books to understand this better.

    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@googlegroups.com.
    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.
  • Rodrigo Ruiz at Jan 30, 2012 at 8:59 pm
    Thank you, but I knew that, I did that (use self to do what I want) before
    I asked the question.

    I'd just like to know why can't I use @first_name instead of
    self.first_name, to me it seems like the same thing inside the model.



    On Mon, Jan 30, 2012 at 6:25 PM, Peter Vandenabeele
    wrote:
    On Mon, Jan 30, 2012 at 9:09 PM, Rodrigo Ruiz wrote:

    Hi, I'd like to know why I can't, or how can I, access attributes like
    that:

    class User < ActiveRecord::Base
    def name
    @first_name + @last_name
    end
    end

    first_name and last_name are user attributes in the database.
    ActiveRecord dynamically defines "accessor methods" on the
    user instance.

    That user instance is accessible as 'self' inside the user instance,
    so you could do

    class User < ActiveRecord::Base
    def name
    self.first_name + self.last_name
    end
    end

    But, in Ruby, when using a _read_ accessor, you could also write it
    without the explicit 'self'.

    class User < ActiveRecord::Base
    def name
    first_name + last_name
    # same as self.first_name + self.last_name
    # if there is no first_name, last_name local variable
    # defined in the scope
    end
    end

    For _write_ accessors, you _must_ put the self in front, that is:

    self.full_name = "#{first_name} #{last_name}"

    And the @first_name you asked about, that is an "instance variable".

    Read one of the many Ruby tutorials or books to understand this better.

    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@googlegroups.com.
    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@googlegroups.com.
    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.
  • Peter Vandenabeele at Jan 30, 2012 at 9:07 pm

    On Mon, Jan 30, 2012 at 9:59 PM, Rodrigo Ruiz wrote:

    Thank you, but I knew that, I did that (use self to do what I want) before
    I asked the question.

    I'd just like to know why can't I use @first_name instead of
    self.first_name, to me it seems like the same thing inside the model.
    They are not:

    @first_name is an "instance variable". You could set it like this:

    @first_name = user.first_name

    but it is not automatically set, and it is also not typically used like
    that.

    A more typical use would be:

    @user = User.find(params[:id])

    and then you can use @user in the controller, but also in the views,
    due to the set-up of Rails.

    self.first_name is a method that is dynamically provided by
    ActiveRecord , based on the available columns for the table
    "users" in the database.

    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@googlegroups.com.
    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.
  • Rodrigo Ruiz at Jan 31, 2012 at 2:29 am
    I thought they where attr_accessor method.

    Thank you for clearing things up for me.

    On Monday, January 30, 2012, Peter Vandenabeele wrote:
    On Mon, Jan 30, 2012 at 9:59 PM, Rodrigo Ruiz wrote:

    Thank you, but I knew that, I did that (use self to do what I want)
    before I asked the question.
    I'd just like to know why can't I use @first_name instead of
    self.first_name, to me it seems like the same thing inside the model.
    They are not:
    @first_name is an "instance variable". You could set it like this:
    @first_name = user.first_name
    but it is not automatically set, and it is also not typically used like that.
    A more typical use would be:
    @user = User.find(params[:id])
    and then you can use @user in the controller, but also in the views,
    due to the set-up of Rails.
    self.first_name is a method that is dynamically provided by
    ActiveRecord , based on the available columns for the table
    "users" in the database.
    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@googlegroups.com.
    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@googlegroups.com.
    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.
  • Frederick Cheung at Jan 31, 2012 at 11:32 am

    On Jan 31, 2:29 am, Rodrigo Ruiz wrote:
    I thought they where attr_accessor method.

    Thank you for clearing things up for me.
    To add a little more info, the attribute values are stored in the
    @attributes / @attributes_cache hashes

    Fred
    On Monday, January 30, 2012, Peter Vandenabeele wrote:> On Mon, Jan 30, 2012 at 9:59 PM, Rodrigo Ruiz wrote:

    Thank you, but I knew that, I did that (use self to do what I want)
    before I asked the question.>> I'd just like to know why can't I use @first_name instead of

    self.first_name, to me it seems like the same thing inside the model.








    They are not:
    @first_name is an "instance variable". You could set it like this:
    @first_name = user.first_name
    but it is not automatically set, and it is also not typically used like that.
    A more typical use would be:
    @user = User.find(params[:id])
    and then you can use @user in the controller, but also in the views,
    due to the set-up of Rails.
    self.first_name is a method that is dynamically provided by
    ActiveRecord , based on the available columns for the table
    "users" in the database.
    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@googlegroups.com.
    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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 30, '12 at 8:10p
activeJan 31, '12 at 11:32a
posts7
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase