Ruby on rails 为什么我能';t在两个表中使用t1:m关联来形成m:m关联?
我是rails的新手,今天下午我在考虑这个想法 这是我的密码: 视频表迁移文件:Ruby on rails 为什么我能';t在两个表中使用t1:m关联来形成m:m关联?,ruby-on-rails,Ruby On Rails,我是rails的新手,今天下午我在考虑这个想法 这是我的密码: 视频表迁移文件: class CreateVideos < ActiveRecord::Migration def self.up create_table :videos do |t| t.string :title t.string :drummer_id t.timestamps end end def self.down drop_table
class CreateVideos < ActiveRecord::Migration
def self.up
create_table :videos do |t|
t.string :title
t.string :drummer_id
t.timestamps
end
end
def self.down
drop_table :videos
end
end
class CreateDrummers < ActiveRecord::Migration
def self.up
create_table :drummers do |t|
t.string :first_name
t.string :video_id
t.timestamps
end
end
没关系
但是
在另一边,什么都没有
Video.find_by_title("JojoVideo1").drummers
=>[]
我检查所有外键是否匹配,但我不知道上面的查询返回的是一个空白数组。我不知道您的代码中有什么错误。但我想建议另一种方法。您可以创建一个连接模型,该模型将为您处理多对多关联。使用连接模型可以更好地控制代码。这可以通过以下方式完成:
class A < ActiveRecord::Base
has_many :cs
has_many :bs, :through => cs
end
class B < ActiveRecord::Base
has_many :cs
has_many :as, :through => cs
end
class C < ActiveRecord::Base
belongs_to :as
belongs_to :bs
end
class Acs
结束
B类cs
结束
C类
每个模型的迁移过程如下所示
class CreateAs < ActiveRecord::Migration
def self.up
create_table :as do |t|
t.string :title
t.timestamps
end
end
def self.down
drop_table :as
end
end
class CreateBs < ActiveRecord::Migration
def self.up
create_table :bs do |t|
t.string :title
t.timestamps
end
end
def self.down
drop_table :bs
end
end
class CreateCs < ActiveRecord::Migration
def self.up
create_table :cs do |t|
t.references :as
t.references :bs
t.timestamps
end
end
def self.down
drop_table :cs
end
end
class CreateAs
另一种方法是只创建一个没有模型的联接表。这可以通过以下方式实现:-
A类 > Drummer.all
=> [#<Drummer id: 1, first_name: "Jojo", video_id: nil, created_at: "2010-12-10 11:04:48", updated_at: "2010-12-10 11:04:48">]
> Video.all
=> [#<Video id: 1, title: "JojoVideo1", drummer_id: "1", created_at: "2010-12-10 11:04:48", updated_at: "2010-12-10 11:04:48">]
> Video.all.first.drummer
=> #<Drummer id: 1, first_name: "Jojo", video_id: nil, created_at: "2010-12-10 11:04:48", updated_at: "2010-12-10 11:04:48">
> Video.all.first.drummers
=> []
Video.all.first.drummer有效,因为它是通过转到视频然后检查其鼓手id找到的。另一个无效,因为它尝试查找具有视频id的鼓手,并且我们创建视频的方式没有设置鼓手的视频id
我们可以用相反的问题构建一对视频/鼓手:
d = Drummer.first
d.create_video(:title => "JojoVideo2")
d.save
新的视频对鼓手来说表现良好,但对鼓手来说表现不佳——因为这一次,鼓手有视频id,但视频没有鼓手id
实际结果是,你应该按照Rohit说的做:)嘿Rohit,谢谢你告诉我如何使用join table进行多对多关联,我已经知道了,但无论如何,谢谢你Hey lain,你就是那个人!现在我知道为什么它不起作用了。谢谢你告诉我如何检查日志文件,里面有很多信息,现在我检查sql查询以了解引擎盖下发生了什么
> Drummer.all
=> [#<Drummer id: 1, first_name: "Jojo", video_id: nil, created_at: "2010-12-10 11:04:48", updated_at: "2010-12-10 11:04:48">]
> Video.all
=> [#<Video id: 1, title: "JojoVideo1", drummer_id: "1", created_at: "2010-12-10 11:04:48", updated_at: "2010-12-10 11:04:48">]
> Video.all.first.drummer
=> #<Drummer id: 1, first_name: "Jojo", video_id: nil, created_at: "2010-12-10 11:04:48", updated_at: "2010-12-10 11:04:48">
> Video.all.first.drummers
=> []
Video Load (0.3ms) SELECT "videos".* FROM "videos"
Drummer Load (0.3ms) SELECT "drummers".* FROM "drummers" WHERE ("drummers"."id" = 1) LIMIT 1
Video Load (0.3ms) SELECT "videos".* FROM "videos"
Drummer Load (0.3ms) SELECT "drummers".* FROM "drummers" WHERE ("drummers".video_id = 1)
d = Drummer.first
d.create_video(:title => "JojoVideo2")
d.save