2016-07-03 36 views
0

关联是我的铁轨跟腱。如何显示来自导轨控制台的关联

我有3个模型和控制器:UserList,Item。用户可以创建usernamepassword。列表可以创建列表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 

on 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拥有一个名为“重要的东西”的列表,其中有“洗碗”项目。

List.create(name: "Important", user_id: 2) #let's say this has id of 1 

Item.create(item_name: "Wash dishes", list_id: 1) 

我假设项目连接到列表和列表已连接到用户。但是,当我键入User.last.name而不是看到“重要”,我得到一个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显示最后一个用户的项目名称或列表名称?

回答

1

你的代码是正确的,但你的假设是错误的。 User.last返回用户记录,因此访问关联记录中的方法时将获得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 
+0

它的工作。谢谢!我只是想了一个后续问题。由于我仍在学习多态,我不确定它是否可行,但可以使用命令User.last.item_name或User.last.name(name = list name)并返回一个值吗? (意思是说,是否有可能直接从用户转到其他相关项目的属性?),还是必须经过上述答案中提到的过程?非常感谢! – Iggy

+0

当然你可以在这里应用多态关联,即'Item'可以同时属于'User'和'List'。 http://guides.rubyonrails.org/association_basics.html#polymorphic-associations应该给予应该给出的例子 – kasperite