Forum: Ruby on Rails JOIN

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73e5f7ab8caf1c742b0339d695382eae?d=identicon&s=25 unknown (Guest)
on 2006-01-13 01:54
(Received via mailing list)
Hi!

For the following example I want to retrieve the last 10 LastEntries for
experis_date >= now.

class User < ActiveRecord::Base
  has_one :last_entries
  def self.last_entry_items
    find(:all,
#    :include => "last_entries",
#    :conditions => "last_entries.expires_date >= now()",
    :order => "id desc",
    :limit => 10)
  end
end

class LastEntrie < ActiveRecord::Base
	belongs_to :user
end

Rails tells me the following:

Mysql::Error: #42S02Unknown table 'last_entries' in where clause: SELECT
* FROM
users WHERE (last_entries.expires_date >= now())  ORDER BY id desc
LIMIT 10

I was expecting to see here an inner join or something. Then I tried
using
:include and rails gave the following error:

uninitialized constant LastEntries

I would apreciate any tip on this subject.

Thanks for you attention,

Paulo Abreu
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-13 03:00
unknown wrote:
> Hi!
>
> For the following example I want to retrieve the last 10 LastEntries for
> experis_date >= now.
>
> class User < ActiveRecord::Base
>   has_one :last_entries
>   def self.last_entry_items
>     find(:all,
> #    :include => "last_entries",
> #    :conditions => "last_entries.expires_date >= now()",
>     :order => "id desc",
>     :limit => 10)
>   end
> end
>
> class LastEntrie < ActiveRecord::Base
> 	belongs_to :user
> end
>
> Rails tells me the following:
>
> Mysql::Error: #42S02Unknown table 'last_entries' in where clause: SELECT
> * FROM
> users WHERE (last_entries.expires_date >= now())  ORDER BY id desc
> LIMIT 10
>
> I was expecting to see here an inner join or something. Then I tried
> using
> :include and rails gave the following error:
>
> uninitialized constant LastEntries
>
> I would apreciate any tip on this subject.
>
> Thanks for you attention,
>
> Paulo Abreu

Try

LastEntrie.find(:all, :condition=>"expires_date >= now()", :order=>"id
desc", :limit=>10)

This will return LastEntrie objects. Your call would try to return User
objects
73e5f7ab8caf1c742b0339d695382eae?d=identicon&s=25 unknown (Guest)
on 2006-01-13 11:03
(Received via mailing list)
>>     :limit => 10)
>> * FROM
>>
> objects
Hi, Thanks for you feedback.

I am not sure if that will join both entities. But assuming it will, I
have two doubts about how to use find:

- if I don't want the join;

- for a :condition like user.valid = true and expires_date >= now()

Paulo Abreu,
This topic is locked and can not be replied to.