I have the following many-to-many relationship:
class Assignment < ActiveRecord::Base
belongs_to :programmer
belongs_to :project
end
class Programmer < ActiveRecord::Base
has_many :assignments
has_many :projects, :through => :assignments
end
class Project < ActiveRecord::Base
has_many :assignments
has_many :programmers, :through => :assignments
end
And in my db:migrate I have the following:
class CreateAssignments < ActiveRecord::Migration
def self.up
create_table :assignments do |t|
t.integer :programmer_id
t.integer :project_id
t.boolean :owner, :default => false
t.timestamps
end
end
def self.down
drop_table :assignments
end
end
This means that I can load a project belonging to a programmer, by doing this:
@my_programmer.projects.find params[:id]
But if you see my migration, each assignment also has a “owner” flag that indicates whether or not the programmer is the owner of that project. My problem is that this query will only give me the project, and not access to the “owner” flag.
So how do I know whether the programmer is the owner? I could do another call to get the collaboration, but it seems stupid as it’s already doing a JOIN on assignments?
Is there a way to get the properties on the :through class, without needing to do a specific database call?
It is quite strange architecture of your database. But you can try this
UPD
Another solution for using eager loading: