Rails4 一对多关系:从子属性中查找父属性parent_id
Rails 4 one-to-many relationship: find parent attribute from child parent_id attribute
我有五个模型:
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
has_many :comments
end
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
has_many :posts
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
class Post < ActiveRecord::Base
belongs_to :calendar
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
comment
table 有以下列:id
、post_id
、user_id
和 body
.
在不同的视图中,例如在 show.html.erb
post 视图中,我需要显示 posted 的 user
名字的相关评论comment
.
换句话说,我正在尝试从 comment.user_id
中检索 user.first_name
。
为此,我在 comment.rb
文件中定义了以下方法:
def self.user_first_name
User.find(id: '#{comment.user_id}').first_name
end
然后我更新了 show.html.erb
post 视图如下:
<h3>Comments</h3>
<% @post.comments.each do |comment| %>
<p>
<strong><%= comment.user_first_name %></strong>
<%= comment.body %>
</p>
<% end %>
当我这样做时,出现以下错误:
NoMethodError in Posts#show
undefined method `user_first_name' for #<Comment:0x007fc510b67380>
<% @post.comments.each do |comment| %>
<p>
<strong><%= comment.user_first_name %></strong>
<%= comment.body %>
</p>
<% end %>
我不太明白为什么我会收到与 Posts#show
相关的错误。
知道如何解决这个问题吗?
替换:
comment.rb
def self.user_first_name
User.find(id: '#{comment.user_id}').first_name
end
与:
comment.rb
delegate :first_name, to: :user, prefix: true
如果这样做,您只需拨打相同的电话 comment.user_first_name
,它就会为您提供用户的名字。如果您不想在用户没有 first_name.
时中断,请添加 , allow_nil: true
您可能还想添加:
has_many :comments
user.rb
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
has_many :comments
end
我有五个模型:
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
has_many :comments
end
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
has_many :posts
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
class Post < ActiveRecord::Base
belongs_to :calendar
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
comment
table 有以下列:id
、post_id
、user_id
和 body
.
在不同的视图中,例如在 show.html.erb
post 视图中,我需要显示 posted 的 user
名字的相关评论comment
.
换句话说,我正在尝试从 comment.user_id
中检索 user.first_name
。
为此,我在 comment.rb
文件中定义了以下方法:
def self.user_first_name
User.find(id: '#{comment.user_id}').first_name
end
然后我更新了 show.html.erb
post 视图如下:
<h3>Comments</h3>
<% @post.comments.each do |comment| %>
<p>
<strong><%= comment.user_first_name %></strong>
<%= comment.body %>
</p>
<% end %>
当我这样做时,出现以下错误:
NoMethodError in Posts#show
undefined method `user_first_name' for #<Comment:0x007fc510b67380>
<% @post.comments.each do |comment| %>
<p>
<strong><%= comment.user_first_name %></strong>
<%= comment.body %>
</p>
<% end %>
我不太明白为什么我会收到与 Posts#show
相关的错误。
知道如何解决这个问题吗?
替换:
comment.rb
def self.user_first_name
User.find(id: '#{comment.user_id}').first_name
end
与:
comment.rb
delegate :first_name, to: :user, prefix: true
如果这样做,您只需拨打相同的电话 comment.user_first_name
,它就会为您提供用户的名字。如果您不想在用户没有 first_name.
, allow_nil: true
您可能还想添加:
has_many :comments
user.rb
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
has_many :comments
end