Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4239900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:05:44+00:00 2026-05-21T03:05:44+00:00

I have the following many-to-many relationship: class Assignment < ActiveRecord::Base belongs_to :programmer belongs_to :project

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:05:45+00:00Added an answer on May 21, 2026 at 3:05 am

    It is quite strange architecture of your database. But you can try this

    class Programmer < ActiveRecord::Base
      has_many :assignments
      has_many :projects, :through => :assignments
    
      def own_projects
        projects.where(:owner => true)
      end
    end
    
    class Project < ActiveRecord::Base
      has_many :assignments
      has_many :programmers, :through => :assignments
    
      def owners
        programmers.where(:owner => true)
      end
    
      def collaborators
        programmers.where(:owner => false)
      end
    end
    
    @my_programmer.own_projects
    @my_project.owners
    @my_project.owners.include? @my_programmer
    @my_project.colaborators
    

    UPD

    Another solution for using eager loading:

    class Programmer < ActiveRecord::Base
      has_many :assignments
      has_many :projects, :through => :assignments
      has_many :own_projects, :class_name => "Project", :through => :assignments, :conditions => ['assignments.owner = ?', true], :source => :project
    end
    
    class Project < ActiveRecord::Base
      has_many :assignments
      has_many :programmers, :through => :assignments
      has_many :owners, :class_name => "Programmer", :through => :assignments, :conditions => ['assignments.owner = ?', true], :source => :programmer
      has_many :collaborators, :class_name => "Programmer", :through => :assignments, :conditions => ['assignments.owner = ?', false], :source => :programmer
    end
    
    @my_programmer.projects.includes(:owners).each do |project|
      project.owner
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say i have the following relationship: class Event < ActiveRecord::Base has_many :tickets end
I have the following class: class Group < ActiveRecord::Base has_many :users belongs_to :leader, :foreign_key
I have a one-to many relationship in Rails: class User < ActiveRecord::Base has_many :activities,
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models: class EventParticipant < ActiveRecord::Base belongs_to :event has_one :user attr_accessible
I've got my models setup for a many-to-many relationship: class Workshop < ActiveRecord::Base has_many
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have the following relationship CType has many Field Field has many Ctype and
I have the following HTML with many of these kinds of links: <a class=button
I have the following models: class Foo has_and_belongs_to_many :bars end class Bar has_and_belongs_to_many :foos

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.