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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Sun, Sep 9, 2012 at 9:45 PM, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
> Try @result.to_yaml to see its attributesI 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 10 September 2012 03:58, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.