Ruby on rails Rails-如何仅在一个方向上关联模型
嗨,伙计们 我是Rails新手,我觉得我肯定错过了一些关键的东西,因为这似乎应该是一个容易解决的问题 我已经设置了Ruby on rails Rails-如何仅在一个方向上关联模型,ruby-on-rails,associations,models,Ruby On Rails,Associations,Models,嗨,伙计们 我是Rails新手,我觉得我肯定错过了一些关键的东西,因为这似乎应该是一个容易解决的问题 我已经设置了页面模型和协作模型(在入门教程的帮助下),并且协作成功地属于页面。我正在尝试应用类似的逻辑来创建另一个模型,Comment,它属于Coord,并且只通过Coord属于Page 我是否使用:至来表示(我认为)只需要向一个方向链接的关联?如第页
页面
模型和协作
模型(在入门教程的帮助下),并且协作
成功地属于页面
。我正在尝试应用类似的逻辑来创建另一个模型,Comment
,它属于Coord
,并且只通过Coord
属于Page
我是否使用:至来表示(我认为)只需要向一个方向链接的关联?如第页
class Page < ActiveRecord::Base
attr_accessible :description, :name
has_many :coords
has_many :comments, :through => :coords
end
页面
class Page < ActiveRecord::Base
has_many :coords
has_many :comments, :through => :coords
end
class页面:coords
结束
Coord
class Coord < ActiveRecord::Base
belongs_to :page
has_many :comments
end
class-Coord
评论
class Comment < ActiveRecord::Base
belongs_to :coord
has_one :page, :through => :coord
end
class注释:coord
结束
使用上述方法,您不需要在注释
表中使用页面id
引用:有一个:coord,:到=>:页面应该在您的案例中工作。您不需要在注释模型中有一个coord\u id:integer,以便它可以将注释引用到coord吗?
class Page < ActiveRecord::Base
has_many :coords
has_many :comments, :through => :coords
end
class Coord < ActiveRecord::Base
belongs_to :page
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :coord
has_one :page, :through => :coord
end