Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 3:与3个组合模型的关联_Ruby On Rails_Ruby On Rails 3_Model_Associations - Fatal编程技术网

Ruby on rails Rails 3:与3个组合模型的关联

Ruby 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,但我更改了它以简化问题。只是一个猜测,但是从用户中删除是否有很

我无法通过关联访问模型。我有三种型号:

User.rb


我想上面的联系有问题,知道吗

尽量避免双方的关系:
属于
有很多/有一个
。 因此,在您的情况下,您应该改变:

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
used
post.user.profile.forename
。(或者对于这种情况下使用BlogIt gem
post.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