Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-如何仅在一个方向上关联模型_Ruby On Rails_Associations_Models - Fatal编程技术网

Ruby on rails Rails-如何仅在一个方向上关联模型

Ruby on rails Rails-如何仅在一个方向上关联模型,ruby-on-rails,associations,models,Ruby On Rails,Associations,Models,嗨,伙计们 我是Rails新手,我觉得我肯定错过了一些关键的东西,因为这似乎应该是一个容易解决的问题 我已经设置了页面模型和协作模型(在入门教程的帮助下),并且协作成功地属于页面。我正在尝试应用类似的逻辑来创建另一个模型,Comment,它属于Coord,并且只通过Coord属于Page 我是否使用:至来表示(我认为)只需要向一个方向链接的关联?如第页

嗨,伙计们

我是Rails新手,我觉得我肯定错过了一些关键的东西,因为这似乎应该是一个容易解决的问题

我已经设置了
页面
模型和
协作
模型(在入门教程的帮助下),并且
协作
成功地
属于
页面
。我正在尝试应用类似的逻辑来创建另一个模型,
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