我有多个表
动作:
id
line_id
devise_id
item_id
注释:
id
action_id
body
项:
id
name
色器件:
id
name
行:
id
name
型号: ActionModel:
belongs_to :devise, :foreign_key => 'devise_id'
belongs_to :item, :foreign_key => 'item_id'
belongs_to :line, :foreign_key => 'line_id'
has_many :comments
CommentModel:
belongs_to :action, :foreign_key => 'item_id'
has_many :items, through: :actions
ItemModel:
has_many :items, dependent: :destroy
has_many :devises, through: :actions
has_many :lines, through: :actions
DeviseModel:
has_many :actions, dependent: :destroy
has_many :items, through: :actions
has_many :lines, through: :actions
LineModel:
has_many :actions, dependent: :destroy
has_many :devises, through: :actions
has_many :lines, through: :actions
has_many :comments, through: :actions
在我的动作控制器中,我希望有这样的东西:
def index
@actions = Action.joins(:item, :comment)
在我的视图中获取action.comment.body
和action.item.name
。
有人可以建议是否有办法吗?
答案 0 :(得分:1)
您需要更改此内容,因为action_id
模型中已有Comment
class Comment
belongs_to :action
end
您可以包含关联
@actions = Action.includes(:item, :comments)
它将获取所有评论和操作项。
你可以致电
action.comments.each do |comment|
# Loop over the comments
comment.body
end
# and
action.item.name