如何从 rails 控制台显示关联
How to display associations from rails console
社团是我的rails致命弱点。
我有 3 个模型和控制器:User
、List
、Item
。
用户可以创建 username
和 password
。列表可以创建列表name
,项目可以创建item_name
理想情况下,一个列表属于一个用户。一个项目属于一个列表。列表有很多项目。用户有很多列表。所以我想出了:
class Item < ActiveRecord::Base
belongs_to :list
delegate :user, to: :list
end
class List < ActiveRecord::Base
belongs_to :user
has_many :items
end
class User < ActiveRecord::Base
has_many :lists
has_many :items, through: :lists
end
在 rails 控制台上,为了确定,我检查了以下列:
2.2.1 :005 > List.column_names
=> ["id", "name", "user_id", "created_at", "updated_at"]
2.2.1 :006 > Item.column_names
=> ["id", "item_name", "list_id", "created_at", "updated_at"]
2.2.1 :007 > User.column_names
=> ["id", "username", "password", "created_at", "updated_at"]
所以我去创建新的用户、项目和列表:
User.create(username: "iggy2", password: "helloworld")
#let's say iggy2 has user_id 2.
我想让 iggy2 有一个名为 "important stuff" 的列表,其中包含项目 "Wash dishes"。
List.create(name: "Important", user_id: 2 ) #let's say this has id of 1
Item.create(item_name: "Wash dishes", list_id: 1)
我假设 Item 连接到 List,List 连接到 User。但是当我输入 User.last.name 时,我看到的不是 "Important",而是 NoMethodError: undefined method 'name'
。我在 List.last.item_name
上也遇到了类似的错误
这是我的架构的样子
create_table "items", force: :cascade do |t|
t.text "item_name"
t.integer "list_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "lists", force: :cascade do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "username"
t.string "password"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
我的代码缺少什么?我的假设错了吗?如何让 User.last.name
甚至 User.last.item_name
显示最后一个用户的项目名称或列表名称?
你的代码是正确的,但你的假设是错误的。 User.last
returns 一条用户记录,因此当您访问关联记录中的方法时,您将得到 NoMethodError
。
我想你想要的是下面的内容:
List.where(user_id: User.last.id).pluck(:name) # Return all list names belong to last user
id = List.find_by(user_id: User.last)
Item.where(list_id: id).pluck(:item_name) # Return all item names belong to last user
社团是我的rails致命弱点。
我有 3 个模型和控制器:User
、List
、Item
。
用户可以创建 username
和 password
。列表可以创建列表name
,项目可以创建item_name
理想情况下,一个列表属于一个用户。一个项目属于一个列表。列表有很多项目。用户有很多列表。所以我想出了:
class Item < ActiveRecord::Base
belongs_to :list
delegate :user, to: :list
end
class List < ActiveRecord::Base
belongs_to :user
has_many :items
end
class User < ActiveRecord::Base
has_many :lists
has_many :items, through: :lists
end
在 rails 控制台上,为了确定,我检查了以下列:
2.2.1 :005 > List.column_names
=> ["id", "name", "user_id", "created_at", "updated_at"]
2.2.1 :006 > Item.column_names
=> ["id", "item_name", "list_id", "created_at", "updated_at"]
2.2.1 :007 > User.column_names
=> ["id", "username", "password", "created_at", "updated_at"]
所以我去创建新的用户、项目和列表:
User.create(username: "iggy2", password: "helloworld")
#let's say iggy2 has user_id 2.
我想让 iggy2 有一个名为 "important stuff" 的列表,其中包含项目 "Wash dishes"。
List.create(name: "Important", user_id: 2 ) #let's say this has id of 1
Item.create(item_name: "Wash dishes", list_id: 1)
我假设 Item 连接到 List,List 连接到 User。但是当我输入 User.last.name 时,我看到的不是 "Important",而是 NoMethodError: undefined method 'name'
。我在 List.last.item_name
这是我的架构的样子
create_table "items", force: :cascade do |t|
t.text "item_name"
t.integer "list_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "lists", force: :cascade do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "username"
t.string "password"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
我的代码缺少什么?我的假设错了吗?如何让 User.last.name
甚至 User.last.item_name
显示最后一个用户的项目名称或列表名称?
你的代码是正确的,但你的假设是错误的。 User.last
returns 一条用户记录,因此当您访问关联记录中的方法时,您将得到 NoMethodError
。
我想你想要的是下面的内容:
List.where(user_id: User.last.id).pluck(:name) # Return all list names belong to last user
id = List.find_by(user_id: User.last)
Item.where(list_id: id).pluck(:item_name) # Return all item names belong to last user