views:

21

answers:

1

Hello all! I've got the following models:

project.rb

has_many :tasks

task.rb

belongs_to :project
has_many :assignments
has_many :users, :through => :assignments

user.rb

has_many :assignments
has_many :tasks, :through => :assignments

assignment.rb

belongs_to :task
belongs_to :user

So for example: Project.first.title #=> "Manhattan" Project.first.tasks.map(&:name) # => ['Find Scientists', 'Find Money', 'Find Location'] Project.first.tasks.first.users.map(&:full_name) #=> ['James Maxwell', 'Evariste Galois', 'Jules Verne']

My first question is: How can I find all the persons' names possibly with symbol to proc in one shot, I tried:

Project.first.tasks.users.full_name #=> AND FAILED Project.first.tasks.map(&:users).full_name #=> AND FAILED Project.first.tasks.map(&:users).map(&:full_name) #=> AND FAILED

Any ideas?

And I think this following question might be in the same ball park:

How can I do a find of Project with conditions that search the 'full_name' attribute of the users its tasks?

Example

Project.all(:include => {:tasks => :users}, :conditions => ['tasks.users.full_name LIKE ?', query]) #this failed

I think the problem is at the 'tasks.users'.

Thanks everyone, have a happy thanksgiving!

A: 

For the first one you'll want to do something like this:

Project.first.tasks.map { |t| t.users.map(&:full_name) }.flatten

The reason for this is that you want to iterate through all the tasks, then all the users in each task. Without the flatten this would give you a 2-dimensional array.

And for the second one your find should be:

Project.all(:include => {:tasks => :users}, :conditions => ['users.full_name LIKE ?', query])

Writing users.full_name implies to the SQL engine that you're looking for the full_name field on the users table.

Ryan Bigg
Nik