Get property of a model data

Rails 3.1.3

This must be a simple matter, but since I am stupid enough to
misunderstand something, it does not work as I want it to.

In a controller, I would like to create AND search for another model if
they match in terms of the properties.

def create
@take = current_user.takes.build(params[:take])

# search for matches
@result = Item.where(:flight_name => @take.flight_name,
                     :flight_num => @take.flight_num,
                     :departure => @take.departure,
                     :check_in => @take.check_in,
                     :weight => @take.weight)

if @result.exists?
  if @result.user_id != current_user.id    #<---HERE!!!!

I need to check if the searched ‘Item’ does not have the same ‘user_id’
with the current logged-in user.
So I need to get the ‘user_id’ field of @result.
But certainly, putting ‘@result.user_id’ gives an error.

How was I supposed to get ‘user_id’ from the @result object?

soichi

The problem may be at exists?

What’s the status of the @result object? Is it nil? Does it have any
value? What do you get if you inspect it or print it? What’s the exact
error you get?

What’s the status of the @result object? Is it nil? Does it have any
value?

Since it passed the if part, ‘if @result.exists?’, @result does exist. I
have confirmed that.

What’s the exact
error you get?

The error is simply

undefined method `user_id’ for

Item has a “belongs_to :user”, right?

In any case, what do you get when you print or inspect @result?

Looking at ruby docs, this is the explanation for the method - Evaluates
to true if this resource is not new? and is found on the remote service.
Using this method, you can check for resources that may have been
deleted between the object’s instantiation and actions on it.

http://apidock.com/rails/ActiveResource/Base/exists%3F

In your situation you should be doing a nil check rather than exists
check.

Pradeep S. wrote in post #1075257:

Item has a “belongs_to :user”, right?

yes

In any case, what do you get when you print or inspect @result?

if I print it,

#ActiveRecord::Relation:0x00000100f24800

is what I get.

On Sun, Sep 9, 2012 at 9:45 PM, Soichi I. [email protected]
wrote:

is what I get.

Try @result.to_yaml to see its attributes

Try @result.to_yaml to see its attributes

I got the following

#ActiveRecord::Relation:0x00000100dedae0

  • !ruby/object:Take
    attributes:
    id: 8
    flight_name: soichi
    flight_num: 1111
    departure: 2012-09-21
    check_in: true
    weight: 5
    created_at: 2012-09-10 02:16:05.691104000 Z
    updated_at: 2012-09-10 02:16:05.691104000 Z
    user_id: 2

sorry, ‘Item’ is actually named ‘Take’ here.

I see, so the object you’re getting is of type Relation.

Just for kicks I tried this in rails console for my own Item class. Same
thing happens to me - no method user_id.

You need to get the item object from the @result object. This works -

item = @result.first
item.user_id

There might be better ways though.

item = @result.first
item.user_id

It worked fine! Thank you very much.
Now I need to study ‘type Relation’ :slight_smile:

soichi

On 10 September 2012 03:58, Soichi I. [email protected] wrote:

item = @result.first
item.user_id

It worked fine! Thank you very much.
Now I need to study ‘type Relation’ :slight_smile:

The reason it did not work originally is that @result is not a single
record, it is all the records that match the where clause. In
practice it may be only one but it will still be returned in,
effectively, an array. Hence the need to use .first to get the first
one.

Colin