Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

has_many :through join table conditions not applied on create

#6358

Rails 3.0.3

Given the following models

class User < ActiveRecord::Base
  has_many :roles
  has_many :owner_roles, :class_name => "Role", :conditions => {"roles.name" => "project_owner"}
  has_many :projects, :through => :roles
  has_many :owned_projects, :through => :owner_roles, :source => :project
end
class Role < ActiveRecord::Base
  belongs_to :user
  belongs_to :project
end
class Project < ActiveRecord::Base
  has_many :roles
  has_many :users, :through => :roles
end

When I attempt to use the owned_projects association to create a project for a user:

User.first.owned_projects.create

I get the error

ActiveRecord::UnknownAttributeError: unknown attribute: roles.name

The expected behavior is that the name attribute on the role record used to join the user to the new project would be set to "project_owner"

Reported by Ehren · February 1st, 2011 @ 07:44 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Ehren
    Ehren
    • Tag changed from 3.0.3, has_many, has_many_through_association, hmt to 3.0.3, bug, bugs, has_many, has_many_through_association, hmt

    February 1st, 2011 @ 07:54 PM

  2. andrehjr (at gmail)
    andrehjr (at gmail)

    try this:

     has_many :owner_roles, :class_name => "Role", :conditions => { :name => "project_owner" }
    

    February 3rd, 2011 @ 12:49 AM

  3. rails
    rails
    • State changed from new to open
    • Tag changed from 3.0.3, bug, bugs, has_many, has_many_through_association, hmt to 303, bug, bugs, has_many, has_many_through_association, hmt

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    May 3rd, 2011 @ 01:00 AM

  4. rails
    rails
    • State changed from open to stale

    May 3rd, 2011 @ 01:00 AM