Ruby on rails Rails 3:与3个组合模型的关联
我无法通过关联访问模型。我有三种型号: User.rbRuby on rails Rails 3:与3个组合模型的关联,ruby-on-rails,ruby-on-rails-3,model,associations,Ruby On Rails,Ruby On Rails 3,Model,Associations,我无法通过关联访问模型。我有三种型号: User.rb 我想上面的联系有问题,知道吗 尽量避免双方的关系:属于有很多/有一个。 因此,在您的情况下,您应该改变: class Post < ActiveRecord::Base ... belongs_to :profile ... end class Posttrue?我正在使用blogit gem,这实际上是归属于:blogger,:polymorphic=>true,但我更改了它以简化问题。只是一个猜测,但是从用户中删除是否有很
我想上面的联系有问题,知道吗 尽量避免双方的关系:
属于
有很多/有一个
。
因此,在您的情况下,您应该改变:
class Post < ActiveRecord::Base
...
belongs_to :profile
...
end
class Post
同样,为了实现这一点,您应该将
profile\u id
添加到posts
表中。我认为您可以通过如下方式定义关联:
class Post < ActiveRecord::Base
...
has_one :profile, :through => :user
end
class Post:用户
结束
然后,您可以获得:post.profile.forename
希望这会有所帮助。我认为Post
有一个:profile
和profile有很多:posts
是错误的。但我不知道如何修复它。是的,我有点玩弄了:post有很多:profiles
,但我得到了一个通用的错误,它似乎是错的。谢谢你的建议。为什么你要使用归属于:user,:polymorphic=>true
?我正在使用blogit gem,这实际上是归属于:blogger,:polymorphic=>true
,但我更改了它以简化问题。只是一个猜测,但是从用户
中删除是否有很多:posts
做了什么,因为它已经在Profile.rb中被引用了?嗨!谢谢你迄今为止的帮助。我不想在帖子中添加profile_id,因为我认为这不会满足我的需要。目前,每个用户都有一个配置文件,每个用户都可以写很多文章。Posts有一个user\u id字段,profiles也有一个user\u id字段。我需要链接这两个字段,最好使用关联。还有其他建议吗?是的,使用post.user.profile.forename
。或者,change在Post
中有一个:profile,through::user
。在后一种情况下,如您所料使用:@post.profile.forename
。这太完美了!我添加了属于:profile
usedpost.user.profile.forename
。(或者对于这种情况下使用BlogIt gempost.blogger.profile.forename
)的极少数人来说)。谢谢你,马克!谢谢你!这感觉更接近,但是属于:用户是多态的,所以我得到的不能有一个通过多态关联“Blogit::Post#profile”的has_-many:through关联“Blogit::Post#blogger”。
-还有其他建议吗?
class Post < ActiveRecord::Base
...
attr_accessible :title, :body, :tag_list, :blogger_id, :coverphoto, :locations_attributes
belongs_to :user, :polymorphic => true
has_and_belongs_to_many :locations
has_one :profile
end
<%= post.profile.forename %>
SQLite3::SQLException: no such column: profiles.post_id: SELECT "profiles".* FROM "profiles" WHERE "profiles"."post_id" = 56 LIMIT 1
class Post < ActiveRecord::Base
...
belongs_to :profile
...
end
class Post < ActiveRecord::Base
...
has_one :profile, :through => :user
end