我想建立一个多态关系accepts_nested_attributes_for.这是代码:
class Contact <ActiveRecord::Base
has_many :jobs, :as=>:client
end
class Job <ActiveRecord::Base
belongs_to :client, :polymorphic=>:true
accepts_nested_attributes_for :client
end
Run Code Online (Sandbox Code Playgroud)
当我试图访问Job.create(..., :client_attributes=>{...}给我NameError: uninitialized constant Job::Client
ruby ruby-on-rails polymorphic-associations nested-attributes
我确实发现了一些关于Rails关联的问题,这些关联有点像我的问题,但对于我的生活,我似乎无法理解如何使用belongs_to多个模型.
这是我打算拥有的表结构:
User
id
Post
id
user_id #foreign key; a post belongs to a User aka "Who created this post"
Comment
id
user_id #foreign key; a comment belongs to a User aka "Who made this comment"
post_id #foreign key; a comment belongs to a Post aka "What post this comment is for"
Run Code Online (Sandbox Code Playgroud)
而协会:
User
has_many :posts
has_many :comments
Post
belongs_to :user
has_many :comments
Comment
belongs_to :user
belongs_to :post
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?