【问题标题】:How to add an model-1 relation to the model-2 object that is created by model-3?如何将模型 1 关系添加到模型 3 创建的模型 2 对象?
【发布时间】:2016-04-04 23:08:11
【问题描述】:

模型用户:

has_many :shipments, dependent: :destroy
has_many :friendships
has_many :friends, through: :friendships
has_many :inverse_friendships, :class_name => 'Friendship', 
                                 :foreign_key => 'friend_id'
has_many :inverse_friends, :through => :inverse_friendships, :source => :user

模特友谊:

belongs_to :user
belongs_to :friend, :class_name => 'User'

模型出货:

belongs_to :user

我需要用户创建一个 Shipment 对象并将另一个用户从其好友列表添加(或以某种方式链接)到该对象。

例如:“User-1”从 Shipment-model 创建对象,并在此过程中(在填写表单字段时)将“User-2”从他的好友列表(友谊模型对象)添加到该 Shipment-model 对象。所以最终的 Shipment-model 对象看起来有点像最后一个例子:

user.shipment.name
user.shipment.price
user.shipment.friend.name

从 05.04 更新: 进行了大量研究并找到了添加另一个模型的类似解决方案,现在应用程序看起来像这样(更新变得强大):

模型用户:

has_many :shipments, dependent: :destroy
has_many :friendships
has_many :friends, through: :friendships
has_many :inverse_friendships, :class_name => 'Friendship', 
                                 :foreign_key => 'friend_id'
has_many :inverse_friends, :through => :inverse_friendships, :source => :user
has_many :shipment_users
has_many :shipments, through: :shipment_users

模特友谊:

belongs_to :user
belongs_to :friend, class_name: "User"

模型出货:

belongs_to :user
belongs_to :friend, class_name: "User"
has_many :shipment_users
has_many :users, through: :shipment_users

Model Shipment_User:

belongs_to :shipment
belongs_to :user

用于创建 Shipment 对象的表单:

<%= form_for(@shipment, html: { multipart: true, role: "form"}) do |f| %>
  <%= f.collection_check_boxes :friend_id, User.all, :id, :name do |cb| %>
    <% cb.label(class: "checkbox") {cb.check_box(class: "checkbox") + cb.text} %>
  <% end %>
<% end %>

发货视图文件:

<h4><%= @shipment.user(:id).name %></h4>
<h4><%= @shipment.user.friend(:id).name %></h4>

目前的结果是: 在表单中,Rails 找到所有现有用户并将它们放在复选框中,但是什么也没发生,并且在视图文件中它显示了创建运输的用户的名称,但对于部分“" 我得到另一个错误:

NoMethodError in ShipmentsController#show
undefined method `friend' for #<User:0x007f1e400e8e08> Did you mean? friends friends=

【问题讨论】:

  • 嗨,欢迎来到堆栈溢出!你做了什么来尝试自己解决这个问题?你用谷歌搜索了什么?当你尝试结果时发生了什么?为什么你对你的尝试不满意?在 Stack Overflow,我们希望您在寻求帮助之前自己尝试一下 :) 我的建议是尝试创建后回调。谷歌,看看你能想出什么。如果您遇到无法克服的错误,请回来告诉我们您尝试了什么,错误是什么,然后我们可以提供帮助:)
  • 在过去的 ~10 个小时里,我一直在观看和搜索从 youtube 到 railscast 的不同教程中的解决方案(我在那里找到了 Friendship 模型/控制器示例),但没有找到任何可以复制的内容示例来自。所以毕竟我决定在这里寻求解决方案。
  • 看起来friend 不是shipment 的关联。装运的朋友到底是什么意思?
  • 太棒了 - 告诉我们您尝试了什么,这样我们就不会重复这种努力! :)
  • 正如我上面所写的,我从 railscast 示例中复制了“友谊”- 之后我不知道从哪里看。现在我已经关注了 Anthony E 的回复,并添加了与 Shipment 的模型关系(正如我在此回复下方所写的那样)。

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

首先friend 不是装运关联。此外,尚不清楚哪个用户朋友会指代。如果您能澄清这一点,将有助于提供更具体的答案。

根据您的代码判断,为什么不添加一个简单的friend_id 列来引用本应成为该货物朋友的用户?在这种情况下,您的 belongs_to 看起来像:

添加friend_id后,外键。在 Shipment 模型中:

belongs_to :friend, class_name: "User"

【讨论】:

  • 谢谢,现在我知道从哪里开始了。我已经添加了“belongs_to :friend, class_name: "User"” - 到 Shipment,“friend_id” - 到发货表和发货表单的下一行(不知道这些是什么意思,我从类似的案例中复制了它们): 但现在我得到下一个错误: # 的未定义局部变量或方法“f”
猜你喜欢
  • 2023-02-16
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
相关资源
最近更新 更多