Monday, January 30, 2012

[Rails] Re: Why can't I access attributes in rails model?

I thought they where attr_accessor method.

Thank you for clearing things up for me.


On Monday, January 30, 2012, Peter Vandenabeele <peter@vandenabeele.com> wrote:
> On Mon, Jan 30, 2012 at 9:59 PM, Rodrigo Ruiz <rodrigo.ruiz7@gmail.com> 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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate