Ruby on rails 3 在Rails 3中没有模型的情况下,如何引用HABTM表中的数据?
因此,我最近在两个模型project和user之间创建了一个HABTM关系 以前,我的项目表中有一个user_id列,它的作用类似于外键。现在有一整张桌子在做这个 但我如何引用具有特定用户id和项目id的项目 例如,我以前的视图中有一部分是这样的:Ruby on rails 3 在Rails 3中没有模型的情况下,如何引用HABTM表中的数据?,ruby-on-rails-3,has-and-belongs-to-many,Ruby On Rails 3,Has And Belongs To Many,因此,我最近在两个模型project和user之间创建了一个HABTM关系 以前,我的项目表中有一个user_id列,它的作用类似于外键。现在有一整张桌子在做这个 但我如何引用具有特定用户id和项目id的项目 例如,我以前的视图中有一部分是这样的: <div class="field"> <%= f.label :project_id %><br /> <%= collection_select(:stage, :proj
<div class="field">
<%= f.label :project_id %><br />
<%= collection_select(:stage, :project_id, Project.where(:user_id => current_user), :id, :name) %>
<br />
</div>
# == 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
# == 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
但是我现在如何从db中提取相同的信息,而HABTM表没有模型?新表称为“projects\u users”
我的项目模型如下所示:
<div class="field">
<%= f.label :project_id %><br />
<%= collection_select(:stage, :project_id, Project.where(:user_id => current_user), :id, :name) %>
<br />
</div>
# == 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
# == 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
我的用户模型如下所示:
<div class="field">
<%= f.label :project_id %><br />
<%= collection_select(:stage, :project_id, Project.where(:user_id => current_user), :id, :name) %>
<br />
</div>
# == 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
# == 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
顺便说一句,在没有模型的情况下,如何从rails控制台编辑该表
谢谢。我想这对你有用。基本上,您为某个用户获取所有项目,然后根据项目id缩小范围
user_id = 3 #example
project_id = 2 #example
User.find(user_id).projects.find(project_id)
如果要手动编辑联接表,可以直接使用这些关系
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 = []
我想这对你有用。基本上,您为某个用户获取所有项目,然后根据项目id缩小范围
user_id = 3 #example
project_id = 2 #example
User.find(user_id).projects.find(project_id)
如果要手动编辑联接表,可以直接使用这些关系
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 = []
我不确定我是否完全理解你的问题,但是Project.where:user\u id=>current\u user应该成为current\u user.projects 要将id为1的用户添加到id为3的项目中
Project.find(3).users << User.find(1)
这就是你一直想做的吗?我不确定我是否完全理解你的问题,但是Project。其中:user\u id=>current\u user应该成为current\u user.projects 要将id为1的用户添加到id为3的项目中
Project.find(3).users << User.find(1)
这就是你一直想做的吗?这条铁路对我帮助很大——它会回答你的问题 这个铁路司机帮了我很多忙-它会回答你的问题 当我运行User.finduser_id.projects.findproject_id时,这是我得到的错误消息:未定义的局部变量或方法User_id,表示“是的,User_id应该是一个包含所需用户id的变量”。我将更新答案。问题是,我需要它来自动检测当前用户+当前项目。当我运行user.finduser\u id.projects.findproject\u id时,这是我收到的错误消息:未定义的局部变量或方法user\u id,表示“是的,user\u id应该是一个包含您想要的用户id的变量”。我将更新答案。问题是我需要它来自动检测当前用户+当前项目。更改为当前用户。项目成功。Thnx。至于第二部分,在控制台上应该是b吗?如果是,则不起作用。是的,您应该使用控制台中的第二部分。它应该会起作用。您确定有使用ID的实例吗?改为试试这个:Project.first.users。我在哪里可以读到更多关于这方面的信息?例如,如何从控制台查看projects\u users表中的所有条目?请检查:如果要从控制台查看projects\u users表中的所有条目,我认为需要使用关联联接模型,如上面链接中所述。我不知道如何在控制台中执行此操作。否则,我唯一的建议就是直接查看DB表。谢谢。这个API链接正是我想要的。导游并没有告诉我所有的细节。但这是真的。非常感谢。更改为当前用户。项目成功。Thnx。至于第二部分,在控制台上应该是b吗?如果是,则不起作用。是的,您应该使用控制台中的第二部分。它应该会起作用。您确定有使用ID的实例吗?改为试试这个:Project.first.users。我在哪里可以读到更多关于这方面的信息?例如,如何从控制台查看projects\u users表中的所有条目?请检查:如果要从控制台查看projects\u users表中的所有条目,我认为需要使用关联联接模型,如上面链接中所述。我不知道如何在控制台中执行此操作。否则,我唯一的建议就是直接查看DB表。谢谢。这个API链接正是我想要的。导游并没有告诉我所有的细节。但这是真的。多谢。