【发布时间】:2012-11-13 16:48:39
【问题描述】:
我无法通过关联访问模型。我有三个模型:
用户.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
has_one :profile
has_many :posts
attr_accessible :email, :password, :password_confirmation, :remember_me
end
个人资料.rb
class Profile < ActiveRecord::Base
belongs_to :user, :dependent => :destroy
has_many :posts
attr_accessible :currentlatitude, :currentlongitude, :forename, :surname, :profpicture, :notes
end
Post.rb
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
我想在帖子标题旁边的帖子索引视图中显示 Profile.forename,但是当我尝试时;
<%= post.profile.forename %>
我刚刚收到以下错误:
SQLite3::SQLException: no such column: profiles.post_id: SELECT "profiles".* FROM "profiles" WHERE "profiles"."post_id" = 56 LIMIT 1
我认为上述关联有问题,知道吗?
【问题讨论】:
-
我认为 Post
has_one :profile和 Profilehas_many :posts是错误的。但我不确定如何解决它。 -
是的,我玩过
:post has_many :profiles,但我得到了一个一般性的错误,它似乎是错误的。感谢您的建议。 -
你为什么使用
belongs_to :user, :polymorphic => true? -
我正在使用 blogit gem,这实际上是
belongs_to :blogger, :polymorphic => true但我更改了它以简化问题。 -
只是一个猜测,但是从
User中删除has_many :posts是否有任何作用,因为它已在 Profile.rb 中引用?
标签: ruby-on-rails ruby-on-rails-3 model associations