开发者

Create/edit using a 3 column join table

As a previous post was asking (Alias table name to facilitate 3 column join table (MySQL or PostgreSQL)), I am working on a join table that joins 3 tables, Project, Employee, Role. The use of the :join_table => "my_join_table" works fine when I want to display information coming from the join tables. However, during the create action, the insert into SQL request is done in two times:

INSERT INTO properties_roles_users (project_id, employee_id) VALUES (11, 14)
and 
INSERT INTO properties_roles_users (role_id, project_id) VALUES (5, 11)

instead of having only one insert into with the 3 fields.

I use the habtm relationship.

Any Idea how to get the INSERT INTO that would have all three ID (employee_id, project_id, role_id)?

[EDIT] Ok, I was using part of the code from the other post so it would make sense in the context... we should then read "employees_projects_roles". Any who, let say I'll keep on going with user, property and role. Here's the code from my view:

<% form_for(@property,:html => { :multipart => true }) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :address %><br />
    <%=开发者_StackOverflow社区 f.text_field :address %>
  </p>
  ( ... code ... )

  <p>
    <%= f.label :role, 'Role' %><br />
    <%= f.collection_select :role_ids, Role.find(:all, :order => 'role'), :id, :role, {}, :multiple => true %>
  </p>

      <%= f.hidden_field :user_ids, :value => current_user.id %>

  <%= render :partial => 'form', :locals => { :f => f } %>

  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

Controller (properties_controller:

def create @property = Property.new(params[:property])

respond_to do |format|
  if @property.save
    flash[:notice] = 'Property was successfully created.'
    format.html { redirect_to(@property) }
    format.xml  { render :xml => @property, :status => :created, :location => @property }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @property.errors, :status => :unprocessable_entity }
  end
end

end

Models:

class User < ActiveRecord::Base
  has_and_belongs_to_many :properties, :join_table => "properties_roles_users"
  has_and_belongs_to_many :roles, :join_table => "properties_roles_users"
  (... other useful stuff ...)
end

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => "properties_roles_users"
  has_and_belongs_to_many :properties, :join_table => "properties_roles_users"
end

class Property < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => "properties_roles_users"
  has_and_belongs_to_many :roles, :join_table => "properties_roles_users"

  attr_accessible (...:different_fields...) :user_ids, :role_ids
end


Got it. It seems that I needed to write an :insert_sql statement in the habtm declaration such as:

has_and_belongs_to_many :users, 
:join_table => "projects_roles_users",
:foreign_key => 'role_id',
:association_foreign_key => 'project_id',
:insert_sql => 'INSERT INTO
            projects_roles_users(project_id,role_id,user_id) 
            VALUES(#{id}, #{role_ids}, #{user_ids})'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜