在我的项目中我陷入了一点:项目应该至少能够使用一种语言,而语言有很多项目。我创建了这样的模型:
class Project < ActiveRecord::Base
belongs_to :developer, counter_cache: true
has_many :languages
validates :developer, presence: true
validates :name, presence: true, uniqueness: true
validates :description, presence: true
end
和:
class Language < ActiveRecord::Base
has_many :projects
end
我向数据库添加了 3 种语言。 现在我应该可以用某种语言创建一个项目了:
p = Project.create :developer_id=>"1", :language_id=>"2", :name=>"Project 1", :description=>"Sample project"
它说:
ActiveRecord::UnknownAttributeError: unknown attribute 'language_id' for Project.
好吧,我有带有这样 ID 的语言,那为什么它不能添加一个。这可能是语言的问题 ->他们没有项目,但有关系:has_many:项目
期待收到你们的来信:
编辑: 现在它看起来像:
class Language < ActiveRecord::Base
has_and_belongs_to_many :projects
end
class Project < ActiveRecord::Base
belongs_to :developer, counter_cache: true
has_and_belongs_to_many :languages
validates :developer, presence: true
validates :name, presence: true, uniqueness: true
validates :description, presence: true
end
我使用了 rails g migration CreateProjectsLanguages project:references language:references
它返回:
p.languages.build(:name => "English")
PG::UndefinedTable: BŁĄD: relacja "languages_projects" nie istnieje
LINE 5: WHERE a.attrelid = '"languages_projects"'::re...
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"languages_projects"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
ActiveRecord::StatementInvalid: PG::UndefinedTable: BŁĄD: relacja "languages_projects" nie istnieje
LINE 5: WHERE a.attrelid = '"languages_projects"'::re...
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"languages_projects"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
看起来找不到它们之间的关系(也许关联表应该看起来不同)
最佳答案
您应该为多对多关系使用关联表。在 Rails 中,您可以使用 has_many :models 映射实体,通过:associative_table_name。
class Project < ActiveRecord::Base
has_many :language_projects
has_many :projects, through: :language_projects
end
class LanguageProject < ActiveRecord::Base
belongs_to :project
belongs_to :language
end
class Language < ActiveRecord::Base
has_many :language_projects
has_many :projects, through: :language_projects
end
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
在您的情况下,您可以为关联表生成模型和迁移:
rails g model LanguageProject project:references language:references
如果您不需要使用关联表作为模型,您可以直接使用 has_and_belongs_to_many 映射实体。
class Project < ActiveRecord::Base
has_and_belongs_to_many :languages
end
class Language < ActiveRecord::Base
has_and_belongs_to_many :projects
end
在这种情况下,您只能生成一个迁移。您只会得到空壳,因此您必须手动编辑文件
rails g migration CreateLanguagesProjectsJoinTable
class CreateLanguagesProjectsJoinTable < ActiveRecord::Migration
def change
create_table :languages_projects, id: false do |t|
t.integer :language_id
t.integer :project_id
end
end
end
那么你应该能够以这种方式关联模型
p = Project.new(:name => 'myproj')
p.languages.build(:name => 'ruby')
p.save
关于ruby-on-rails - RAILS 我应该使用什么样的关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34252442/