我可以获得正确的数据以在我的 ERB 中显示,但我没有遵循标准。你能帮忙吗?
我有两个模型:用户和交易。 Transactions 表有 user_id 列,用于存储负责事务的用户的 id。
在交易的index.html.erb上,这是自动生成的代码:
<td><%=h transaction.user_id %></td>
由于我希望显示用户名,因此在我的第一种方法中,我将其转换为:
# Index.html.erb
# First approach
<td><%=h User.find(:first, :conditions => ["id =?", transaction.user_id]).firstname %></td>
这让我正确地得到了名字,但我不确定这是正确的这样我在 user.rb (模型文件)中创建了这个方法:
# user.rb
# Second approach
def find_by_id id
@firstname = User.find(:first, :conditions => ["id =?", id]).firstname
return @firstname
end
并将 index.html.erb 中的行修改为:
# Index.html.erb
# Second approach
<td><%=h User.find_by_id(transaction.user_id) %></td>
但这并没有给我用户的名字,而是输出类似于 #< ;User:0x000000021ecf60>
我不确定:a) 为什么会发生这种情况以及如何从方法中获取返回值以正确打印& b)如果我即使在第二种打印用户名的方法中也遵循标准。
I have two models: user and transaction. Transactions table has user_id column for storing the id of the user responsible for transaction.
On the index.html.erb for transactions this was the auto generated code:
<td><%=h transaction.user_id %></td>
Since I want user's name to be displayed, in my first approach I converted this to:
# Index.html.erb
# First approach
<td><%=h User.find(:first, :conditions => ["id =?", transaction.user_id]).firstname %></td>
This got me the firstname correctly, but I was not sure this was the right way so I created this method in user.rb (model file):
# user.rb
# Second approach
def find_by_id id
@firstname = User.find(:first, :conditions => ["id =?", id]).firstname
return @firstname
end
And modified the line in index.html.erb to:
# Index.html.erb
# Second approach
<td><%=h User.find_by_id(transaction.user_id) %></td>
But this does not give me user's firstname and instead the output is something like #<User:0x000000021ecf60>
I am not sure: a) Why this is happening and how to get the return from method to print correctly & b) If I am following standards even in second approach to getting the username printed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您在事务模型中正确设置了事务和用户之间的关联:
那么您可以简单地执行以下操作:
您的“find_by_id”方法不起作用的原因是因为您将其创建为实例方法。因此,当您调用
User.find_by_id
时,它使用普通的 Rails“魔术方法”(一点也不神奇 - 它只是使用method_missing
)来查找记录。我认为,当您拥有所有这些面向对象的可爱功能时,重写此方法以仅返回用户的名字是非常糟糕的设计。Assuming you have your association between transactions and users setup properly in the transaction model:
Then you can simply do:
The reason your 'find_by_id' method didn't work is because you created it as an instance method. So, when you called
User.find_by_id
, it used the normal Rails 'magic method' (not so magic at all - it just usesmethod_missing
) for finding records. I would argue that it is very bad design to override this method to just return the first name of the user when you've got all this object-oriented loveliness around.是的,您可能忘记将
belongs_to
关联插入到 transaction.rb 模型中。确保表事务有一个字段
user_id
。更多相关信息:http://guides.rubyonrails.org/association_basics.html
Yes, you probably forgot to insert
belongs_to
association to transaction.rb model.Be sure that table transaction has a field
user_id
.More on this here: http://guides.rubyonrails.org/association_basics.html