Rails - 使用belongs_to时未生成符号

发布于 2024-11-01 10:41:09 字数 829 浏览 0 评论 0原文

我有一个按以下方式设置的模型 Tran:

class Tran < ActiveRecord::Base
    has_many :transaction_users, :dependent => :destroy, :class_name => 'TransactionUser'
    belongs_to :submitting_user, :class_name => 'User'
    belongs_to :buying_user, :class_name => 'User'

现在,在我看来,我正在尝试访问 :submitting_user_id,因为我已经设置了外键,但 Rails 对我大喊大叫并说找不到该符号。我不应该有权访问它吗?如果没有,我怎样才能获得访问权限? 查看:

<%= nested_form_for(@tran, :url => trans_path) do |f| %>
    <div class="field">
        <%= f.label "Buyer" %>
        <%= f.select :submitting_user_id, options_from_collection_for_select(User.active_users, 'id', 'full_name') %>
    </div>

错误:

undefined method `submitting_user_id' for #<Tran:0x7f6713032fb0>

I have a model Tran that is setup in the following way:

class Tran < ActiveRecord::Base
    has_many :transaction_users, :dependent => :destroy, :class_name => 'TransactionUser'
    belongs_to :submitting_user, :class_name => 'User'
    belongs_to :buying_user, :class_name => 'User'

Now, in my view, I am trying to get access to :submitting_user_id, since I have set up the foreign key, but rails yells at me and says it can't find the symbol. Shouldn't I have access to it? If not, how can I get access?
View:

<%= nested_form_for(@tran, :url => trans_path) do |f| %>
    <div class="field">
        <%= f.label "Buyer" %>
        <%= f.select :submitting_user_id, options_from_collection_for_select(User.active_users, 'id', 'full_name') %>
    </div>

Error:

undefined method `submitting_user_id' for #<Tran:0x7f6713032fb0>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2024-11-08 10:41:09

该字段不会自动为您生成,您必须通过迁移将 submitting_user_id 字段添加到您的表中。

This isn't automatically generated for you, you must have a submitting_user_id field added to your table by a migration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文