association extensions question

Having a brain fart on on this one, maybe someone can set me straight

i have a model that can be requested by and assigned to a user so i have an 'assigned_to_id' column and a 'requested_by_id' column. Now originally i had setup the models as:

class Thing < ActiveRecord::Base belongs_to :requester, :class_name => "User", :foreign_key => "requested_by_id" belongs_to :assignee, :class_name => "User", :foreign_key => "assigned_to_id" ... end

class User < ActiveRecord::Base has_many :requested_things, :class_name => "Thing", :foreign_key => "requested_by_id" has_many :assigned_things, :class_name => "Thing", :foreign_key => "assigned_to_id" end

class User < ActiveRecord::Base has_many :things do    def requested      find(:all, :conditions => 'requested_by_id = #{id}'    end    def assigned      find:all, :conditions => 'assigned_to_id = #{id}'    end end

but attempting to do

@user.things.requested results in:

SELECT * FROM things WHERE (things.user_id = 1 AND (assigned_to_id = #{id}))

where i want

SELECT * FROM pick_requests WHERE (things.assigned_to_id = 1)

so is there a way to do what i am trying to do or should i just stick with the first attempt?

Hello Chris,

Having a brain fart on on this one, maybe someone can set me straight

i have a model that can be requested by and assigned to a user so i have an 'assigned_to_id' column and a 'requested_by_id' column. Now originally i had setup the models as:

[...]

class User < ActiveRecord::Base has_many :things do    def requested      find(:all, :conditions => 'requested_by_id = #{id}'

There's a missing parenthesis. Paste error, I'm guessing.

   end    def assigned      find:all, :conditions => 'assigned_to_id = #{id}'    end end

but attempting to do

@user.things.requested results in:

SELECT * FROM things WHERE (things.user_id = 1 AND (assigned_to_id = #{id}))

Are you sure ? Doesn't it generate :

SELECT * FROM things WHERE (things.user_id = 1 AND (requested_by_id = #{id}))

where i want

SELECT * FROM pick_requests WHERE (things.assigned_to_id = 1)

Are sure this SQL statement is correct ?

so is there a way to do what i am trying to do or should i just stick with the first attempt?

It's not clear to me what you attempt to do, what is pick_requests ? where does it come from ?

   -- Jean-François.

good catches on the typos. i was trying to be a bit abstract and missed a couple of things

Hello Chris,

> Having a brain fart on on this one, maybe someone can set me > straight > > i have a model that can be requested by and assigned to a > user so i have an 'assigned_to_id' column and a 'requested_by_id' > column. Now originally i had setup the models as: [...] > class User < ActiveRecord::Base > has_many :things do > def requested > find(:all, :conditions => 'requested_by_id = #{id}'

There's a missing parenthesis. Paste error, I'm guessing.

yes, bad paste.

> end > def assigned > find:all, :conditions => 'assigned_to_id = #{id}' > end > end > > but attempting to do > > @user.things.requested results in: > > SELECT * FROM things WHERE (things.user_id = 1 AND (assigned_to_id = #{id}))

Are you sure ? Doesn't it generate :

SELECT * FROM things WHERE (things.user_id = 1 AND (requested_by_id = #{id}))

another bad paste, you are correct.

> where i want > > SELECT * FROM pick_requests WHERE (things.assigned_to_id = 1)

Are sure this SQL statement is correct ?

another bad paste, should be

SELECT * FROM things WHERE (things.assigned_to_id = 1)