如何在没有Rails 3中的模型的情况下引用HABTM表中的数据?

mar*_*ion 1 has-and-belongs-to-many ruby-on-rails-3

所以我最近在两个模型(项目和用户)之间创建了一个HABTM关系.

之前,我在项目表中有一个user_id列 - 它就像一个外键.现在有一整个表可以做到这一点.

但是,如何引用具有特定user_id和project_id的项目?

例如,我曾经有一段我的观点看起来像这样:

<div class="field">
        <%= f.label :project_id %><br />
        <%= collection_select(:stage, :project_id, Project.where(:user_id => current_user), :id, :name) %>
        <br />
    </div>
Run Code Online (Sandbox Code Playgroud)

但是,我现在如何从数据库中提取相同的信息,而没有HABTM表的模型?新表称为"projects_users".

我的项目模型如下所示:

# == Schema Information
# Schema version: 20101125223049
#
# Table name: projects
#
#  id          :integer         not null, primary key
#  name        :string(255)
#  description :string(255)
#  notified    :boolean
#  created_at  :datetime
#  updated_at  :datetime
#

class Project < ActiveRecord::Base

  has_and_belongs_to_many :users
  has_many :stages, :dependent => :destroy
  has_many :uploads
  has_many :comments

  #before_validation { |project| project.user = Authorization.current_user unless project.user }

end
Run Code Online (Sandbox Code Playgroud)

我的用户模型如下所示:

# == Schema Information
# Schema version: 20101124095341
#
# Table name: users
#
#  id                   :integer         not null, primary key
#  email                :string(255)     default(""), not null
#  encrypted_password   :string(128)     default(""), not null
#  password_salt        :string(255)     default(""), not null
#  reset_password_token :string(255)
#  remember_token       :string(255)
#  remember_created_at  :datetime
#  sign_in_count        :integer         default(0)
#  current_sign_in_at   :datetime
#  last_sign_in_at      :datetime
#  current_sign_in_ip   :string(255)
#  last_sign_in_ip      :string(255)
#  created_at           :datetime
#  updated_at           :datetime
#  username             :string(255)
#  role                 :string(255)
#

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, and :lockable
  devise :database_authenticatable, :registerable, :timeoutable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_and_belongs_to_many :projects
  has_many :stages
  has_many :uploads
  has_many :comments
  has_many :assignments
  has_many :roles, :through => :assignments

  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end  
end
Run Code Online (Sandbox Code Playgroud)

另外......如何从rails控制台编辑该表 - 没有模型?

谢谢.

rwi*_*ams 5

我认为这对你有用.基本上你是抓住某个用户的所有项目,然后通过project_id缩小它.

user_id = 3 #example
project_id = 2 #example
User.find(user_id).projects.find(project_id)
Run Code Online (Sandbox Code Playgroud)

如果要手动编辑连接表,可以直接使用关系.

project = Project.create
user = User.first

#add a new row to the join table for the user_id,project_id
user.projects << project 

#delete all records from the join table referencing this user.
user.projects = []  
Run Code Online (Sandbox Code Playgroud)