Rails - 使用belongs_to时未生成符号
我有一个按以下方式设置的模型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该字段不会自动为您生成,您必须通过迁移将
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.