如何访问ActiveRecords关联的对象

时间:2018-12-21 09:19:42

标签: ruby-on-rails activerecord

我有两个表,qip_changes和mac_addresses,它们具有一个belongs_to关系。当我尝试从QipChange类对象访问mac_address表时,遇到“对于nil:NilClass的未定义方法`MyDbColumName ='”

我可以获取我的数据,但是我无法写入关联的表:

2.4.1 :044 > i = QipChange.first
QipChange Load (0.5ms)  SELECT  "qip_changes".* FROM "qip_changes" ORDER BY 
"qip_changes"."id" ASC LIMIT $1  [["LIMIT", 1]]
 => #<QipChange id: 2, created_at: "2018-08-21 08:31:48", updated_at: 
"2018-08-21 08:31:48", tenant: "BMC-Test", object_type: "Subnet", action: 
"add", object_data: "blabalalaalla", implementation_status: "started", 
server_response: "", user_id: 1, user_cache: "test"> 
2.4.1 :045 > i.mac_address
MacAddress Load (0.6ms)  SELECT  "mac_addresses".* FROM "mac_addresses" 
WHERE "mac_addresses"."qip_change_id" = $1 LIMIT $2  [["qip_change_id", 2], 
["LIMIT", 1]]
 => nil 
2.4.1 :046 > i.mac_address.mac_address = "asocnasc"
NoMethodError: undefined method `mac_address=' for nil:NilClass
    from (irb):46

我想我只是使用错误的语法来访问关联的表,但是我无法在网上找到某些方法。

我的模特:

class QipChange < ApplicationRecord

  belongs_to :user
  has_one :mac_address
end

class MacAddress < ApplicationRecord
  belongs_to :qip_change
end

来自psql:

ipmatedevel=# \d mac_addresses
                                       Table "public.mac_addresses"
    Column     |            Type             |                         Modifiers
---------------+-----------------------------+------------------------------------------------------------
 id            | bigint                      | not null default nextval('mac_addresses_id_seq'::regclass)
 mac_address   | character varying           |
 created_at    | timestamp without time zone | not null
 updated_at    | timestamp without time zone | not null
 qip_change_id | bigint                      |
Indexes:
    "mac_addresses_pkey" PRIMARY KEY, btree (id)
    "index_mac_addresses_on_qip_change_id" btree (qip_change_id)
Foreign-key constraints:
    "fk_rails_8002186396" FOREIGN KEY (qip_change_id) REFERENCES qip_changes(id)

qipmatedevel=# \d qip_changes
                                           Table "public.qip_changes"
        Column         |            Type             |                        Modifiers                         
-----------------------+-----------------------------+----------------------------------------------------------
 id                    | bigint                      | not null default nextval('qip_changes_id_seq'::regclass)
 created_at            | timestamp without time zone | not null
 updated_at            | timestamp without time zone | not null
 tenant                | character varying           | 
 object_type           | character varying           | 
 action                | character varying           | 
 object_data           | character varying           | 
 implementation_status | character varying           | 
 server_response       | character varying           | 
 user_id               | bigint                      | 
 user_cache            | character varying           | not null
Indexes:
    "qip_changes_pkey" PRIMARY KEY, btree (id)
    "index_qip_changes_on_user_id" btree (user_id)
Foreign-key constraints:
    "fk_rails_3207a22986" FOREIGN KEY (user_id) REFERENCES users(id)
Referenced by:
    TABLE "mac_addresses" CONSTRAINT "fk_rails_8002186396" FOREIGN KEY (qip_change_id) REFERENCES qip_changes(id)

编辑:如果我的协会在运作,请问我。由于一旦输入“ i.mac_address”就会产生查询,看来这是可行的,对吗?

编辑:

i.create_mac_address(mac_address: "aconasic")

可以用来创建mac_address。我需要做什么才能从i开始访问它?

创建mac_address后,

可与以下各项配合使用:

i.mac_address.mac_address

1 个答案:

答案 0 :(得分:0)

如果要更新Rails中的属性,则需要做

 TABLE_NAME.update(ATTRIBUTENAME: "")

例如,如果要更新mac_address,则需要做

i.mac_address.update(mac_address: "asocnasc")

在您发布的查询中,这两个表之间似乎没有关联

如果要为qipChange创建新的mac_address,则需要这样做

i.create_mac_address(mac_address: MAC_ADDRESSS) here i is your QIPCHANGE