Ruby on rails 3.2 如何从另一个表中检索值?
让我来解释一下情况。。!! 我已使用Desive进行身份验证。数据库中的设计表命名为“用户”。现在我有了另一个名为“book”的表,它存储用户idRuby on rails 3.2 如何从另一个表中检索值?,ruby-on-rails-3.2,Ruby On Rails 3.2,让我来解释一下情况。。!! 我已使用Desive进行身份验证。数据库中的设计表命名为“用户”。现在我有了另一个名为“book”的表,它存储用户id class CreateBooks < ActiveRecord::Migration def change create_table :books do |t| t.integer "user_id", :limit =>5 t.string "book_name", :limit => 50 t.integer "e
class CreateBooks < ActiveRecord::Migration
def change
create_table :books do |t|
t.integer "user_id", :limit =>5
t.string "book_name", :limit => 50
t.integer "edition", :limit => 5
t.string "author", :limit => 30
t.string "branch", :limit => 30
t.string "publisher", :limit => 50
t.integer "year", :limit => 10
t.text "details"
t.timestamps
end
add_index :books, "user_id"
end
end
在视图中
<tr>
<th>S.No</th>
<th>Book Name</th>
<th>Year </th>
<th>Owner id</th>
<th>Owner Details</th>
</tr>
</thead>
<tbody>
<% @books.each do |b| %>
<tr>
<td><%= b.id%></td>
<td><%= b.book_name%></td>
<td><%= b.year%></td>
<td><%= b.user_id%></td>
</tr>
<%end%>
美国号
书名
年
所有者id
所有者详细信息
现在,在所有者详细信息下,我想显示用户详细信息。我不知道如何显示它?看到这个了吗
如果您给出关系属于且有许多
比如说
class User < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :user
end
@user = @book.user
因此,在您的books表中,用户id会自动出现
例如,您还可以从book中获取用户详细信息
class User < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :user
end
@user = @book.user
我想你没有了解实际情况。实际上,当用户(店主)登录时,用户的id会在添加新书时自动保存到book表中(我为店主提供了添加、删除和编辑其书籍记录的权限)。我的问题是如何通过此用户id(保存在book table n中,由Deviate创建)检索用户详细信息(即SUser表的数据)。我还想提醒您Deviate的表不同,Shopper的表也不同。这两个表之间也存在一对一的关系。表和用户表之间的多对多关系(designe)。我曾想过这样做v=Book.user\u id
a=SUser.find\u by\u user\u id(v)
b=a.address
@user = @book.user