[Rails] Re: Re: Noob - Associated Tables Find Query

Chris Hall
Fri, 13 Jan 2006 11:54:44 -0000

blasted typos...

class User < ActiveRecord::Base
       has_many :predictions, :include => :match # i originally had
:matches, that was a typo
end

@user = User.find(session[:user_id])
@cutoff_date = Time.now

# combination of Jan''s and my suggestion
@oldpredictions = @user.predictions.find(:all, :include =>
:match,:conditions => ["
match.match_date <= ?", @cutoff_date])

sorry about that.  this should fix it.

Chris

On 1/12/06, Pete Baldwin <pucbaldwin@yahoo.co.uk>
wrote:
>
> Jan and Chris, I really appreciate your responses.
>
> I went with Chris''s alternatives, and have ended up with the
following
>
> class User < ActiveRecord::Base
>         has_many :predictions, :include => :matches
> end
>
> class Match < ActiveRecord::Base
>         has_many :predictions
> end
>
> class Prediction < ActiveRecord::Base
>         belongs_to :user
>         belongs_to :match
> end
>
> ---controller---
> @user = User.find(session[:user_id])
> @cutoff_date = Time.new
>
>
> @oldpredictions = @user.predictions.find(:all, :joins =>
"matches",
> :conditions => ["predictions.match_id = match.id and
match.match_date <> ?", @cutoff_date])
>
> ----
>
> But its giving me the following error...
>
> Mysql::Error: Unknown table ''predictions'' in where
clause: SELECT * FROM
> predictions  matches WHERE (predictions.user_id = 2 AND
> (predictions.match_id = match.id and match.match_date <=
''2006-01-12
> 18:34:14''))
>
> and if I load up the console, and go
>
> @user = User.find(1)
> @user.predictions
>
> it gives me the following error
>
> ActiveRecord::ConfigurationError: Association was not found; perhaps you
> misspelled it?  You specified :include => :matches
>
> Is there something wrong with the models?
>
> Cheers again,
>
> Pete
>
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...