[英]Rails - How to associate models in one direction only
Ahoy guys, 大家好
I'm new to Rails, and I feel like I'm definitely missing something crucial here, because it seems like this should be an easily solvable problem. 我是Rails的新手,我觉得我肯定在这里缺少重要的东西,因为看来这应该是一个容易解决的问题。
I've set up a Page
model and a Coord
model (with help from the getting started tutorial), and Coord
successfully belongs_to
Page
. 我已经建立了一个Page
模型和一个Coord
模型(在入门教程的帮助下),并且Coord
成功地belongs_to
Page
。 I'm trying to apply similar logic to make another model, Comment
, belong to Coord
, and only belong to Page
via Coord
. 我正在尝试应用类似的逻辑来制作另一个模型Comment
,该模型属于Coord
,并且仅属于Page
via Coord
。
Do I use :through
for an association that (I think) only needs to link in one direction? 我是否使用:through
关联(我认为)仅需要在一个方向上进行链接? As in Page < Coord < Comment? 如在页面<坐标<评论? At the moment I have: 目前,我有:
class Page < ActiveRecord::Base
attr_accessible :description, :name
has_many :coords
has_many :comments, :through => :coords
end
Coord model: 坐标模型:
class Coord < ActiveRecord::Base
belongs_to :page
has_many :comments
attr_accessible :coordinates, :x, :y
validates :x, :presence => true
validates :y, :presence => true
end
Then the Comment model: 然后是Comment模型:
class Comment < ActiveRecord::Base
belongs_to :coord
belongs_to :page
attr_accessible :body
end
I still keep getting errors about comments
being an undefined method, or an association not being defined. 我仍然不断收到有关comments
是未定义方法或未定义关联的错误。 Apologies if this is a common question, I don't personally know anyone who knows Rails, and the documentation only has examples too far removed from mine (to my knowledge). 抱歉,如果这是一个常见问题,我个人不认识Rails,并且该文档仅提供了一些与我相去甚远的示例(据我所知)。 Thanks 谢谢
Edit: added DB schema 编辑:添加了数据库架构
ActiveRecord::Schema.define(:version => 20120712170243) do
create_table "comments", :force => true do |t|
t.text "body"
t.integer "coord_id"
t.integer "page_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "comments", ["coord_id"], :name => "index_comments_on_coord_id"
add_index "comments", ["page_id"], :name => "index_comments_on_page_id"
create_table "coords", :force => true do |t|
t.string "coordinates"
t.integer "x"
t.integer "y"
t.integer "page_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "coords", ["page_id"], :name => "index_coords_on_page_id"
create_table "pages", :force => true do |t|
t.string "name"
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
Page 页
class Page < ActiveRecord::Base
has_many :coords
has_many :comments, :through => :coords
end
Coord 协调
class Coord < ActiveRecord::Base
belongs_to :page
has_many :comments
end
Comment 评论
class Comment < ActiveRecord::Base
belongs_to :coord
has_one :page, :through => :coord
end
Using the above, you don't need page_id
in the comments
table. 使用以上内容,您不需要在comments
表中使用page_id
。
Reference: A Guide to Active Record Associations 参考: Active Record关联指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.