Alfo
Alfo

Reputation: 4819

Weird Issue accessing properties in Rails

I'm probably being stupid, but:

@activation_token = ActivationToken.where(:token => params[:id])
render(:text => @activation_token.inspect)

Works, and gives:

[#<ActivationToken id: 7, user_id: 21, token: "f4c3cb7c7629344e8261bf16e4a99900", used: false, created_at: "2013-01-25 10:48:57", updated_at: "2013-01-25 10:48:57">]

But

@activation_token.user_id

Throws:

undefined method `user_id' for #<ActiveRecord::Relation:0x007fbfe414ff18>

What am I missing?

Upvotes: 0

Views: 22

Answers (1)

shweta
shweta

Reputation: 8169

Try:

@activation_token.first.user_id

It is giving collection of objects, if you want to access @activation_token.user_id, Use:

@activation_token = ActivationToken.where(:token => params[:id]).first
# or
@activation_token = ActivationToken.find_by_token(params[:id])

Upvotes: 1

Related Questions