Rails 3.1.3

This must be a simple matter, but since I am stupid enough to
misunderstand something.


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

--
Posted via http://www.ruby-forum.com/.

--
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 https://groups.google.com/groups/opt_out.

Search Discussions

  • Pradeep Singh at Sep 10, 2012 at 2:23 am
    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?

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 https://groups.google.com/groups/opt_out.
  • Soichi Ishida at Sep 10, 2012 at 2:27 am

    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

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 https://groups.google.com/groups/opt_out.
  • Pradeep Singh at Sep 10, 2012 at 2:27 am
    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.

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 https://groups.google.com/groups/opt_out.
  • Pradeep Singh at Sep 10, 2012 at 2:32 am
    Item has a "belongs_to :user", right?

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

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 https://groups.google.com/groups/opt_out.
  • Soichi Ishida at Sep 10, 2012 at 2:53 am

    Pradeep Singh 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.

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 https://groups.google.com/groups/opt_out.
  • Javier Quarite at Sep 10, 2012 at 2:48 am

    On Sun, Sep 9, 2012 at 9:45 PM, Soichi Ishida wrote:

    Pradeep Singh 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.
    Try @result.to_yaml to see its attributes

    --
    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 https://groups.google.com/groups/opt_out.
  • Soichi Ishida at Sep 10, 2012 at 2:51 am
    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

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 https://groups.google.com/groups/opt_out.
  • Soichi Ishida at Sep 10, 2012 at 2:54 am
    sorry, 'Item' is actually named 'Take' here.

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 https://groups.google.com/groups/opt_out.
  • Pradeep Singh at Sep 10, 2012 at 2:55 am
    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.

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 https://groups.google.com/groups/opt_out.
  • Soichi Ishida at Sep 10, 2012 at 2:58 am

    item = @result.first
    item.user_id

    It worked fine! Thank you very much.
    Now I need to study 'type Relation' :)

    soichi

    --
    Posted via http://www.ruby-forum.com/.

    --
    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 https://groups.google.com/groups/opt_out.
  • Colin Law at Sep 10, 2012 at 7:33 am

    On 10 September 2012 03:58, Soichi Ishida wrote:
    item = @result.first
    item.user_id

    It worked fine! Thank you very much.
    Now I need to study 'type Relation' :)
    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

    --
    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 https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedSep 10, '12 at 2:01a
activeSep 10, '12 at 7:33a
posts12
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase