[英]undefined method 'id_tag' for ActiveRecord_Associations_CollectionProxy
In my CrawlProduct class I want to get the id_tag from the table ProductInfo: 在我的CrawlProduct类中,我想从表ProductInfo中获取id_tag:
class CrawlProduct < ActiveRecord::Base
belongs_to :crawl_link
css_id = self.crawl_link.domain.product_infos.id_tag
end
The table CrawlProducts
has a foreign key crawl_link
and the table CrawlLinks
has a foreign key domain
which comes from the table Domain
of course. 表
CrawlProducts
有一个外键crawl_link
,而表CrawlLinks
有一个外键domain
,这当然来自表Domain
。 And ProductInfo belongs to domain. 并且ProductInfo属于域。
class CrawlProduct < ActiveRecord::Base
belongs_to :crawl_link
end
class CrawlLink < ActiveRecord::Base
belongs_to :domain
has_one :crawl_product
end
class Domain < ActiveRecord::Base
has_many :crawl_links
has_many :product_infos
end
class ProductInfo < ActiveRecord::Base
belongs_to :domain
end
But at the end, I always receive the following error: 但是最后,我总是收到以下错误:
undefined method `id_tag' for #<ProductInfo::ActiveRecord_Associations_CollectionProxy:0x0055d72e423640>
How can I access the id_tag
? 如何访问
id_tag
? There is no column missing as far as I know. 据我所知,没有一栏遗漏。
Edit : after the answers I get a different error. 编辑 :答案后,我得到一个不同的错误。
NameError: undefined local variable or method `id_tag' for #<CrawlProduct:0x0055d72fcd9fe0>
I don't know why I'm getting this problem. 我不知道为什么我遇到这个问题。 Here is how I create the id_tag:
这是我创建id_tag的方法:
class Domain < ActiveRecord::Base
domain = Domain.create(domain: 'https://www.example.com')
product_info = ProductInfo.create(domain: domain, id_tag: 'some content')
end
self.crawl_link.domain.product_infos
is a collection of instances, so you can't call instance methods on it, to call it on each item you should either iterate collection with each
or collect ids with map
. self.crawl_link.domain.product_infos
是实例的集合,因此您不能在其上调用实例方法,要在每个项目上调用它,您应该迭代each
集合或使用map
收集ID。 To collect just ids without creating instances you can use pluck
: 要收集ID而不创建实例,可以使用
pluck
:
self.crawl_link.domain.product_infos.pluck(:id_tag)
self.crawl_link.domain.product_infos will return a group of objects so, you cannot retrieve id_tag(a field) from a collection. self.crawl_link.domain.product_infos将返回一组对象,因此,您无法从集合中检索id_tag(一个字段)。
Instead you can use,. 相反,您可以使用。
class CrawlProduct < ActiveRecord::Base
belongs_to :crawl_link
def some_action
css_ids = self.crawl_link.domain.product_infos.pluck(:id_tag)
(or)
css_ids = self.crawl_link.domain.product_infos.map(&:id_tag)
end
end
I think you're accessing product_infos
incorrectly. 我认为您错误地访问了
product_infos
。 You need to specify which one you want, and then access the id_tag from it. 您需要指定所需的那个,然后从中访问id_tag。
Rails is correctly telling you that Rails正确地告诉您
undefined method `id_tag' for #<ProductInfo::ActiveRecord_Associations_CollectionProxy:0x0055d72e423640>
because the relationship is has_many. 因为关系是has_many。
If you wanted to just access the first one the database pulls out (in no specific order, unless you specify), you could just do 如果您只想访问数据库提取的第一个数据库(除非指定,否则不指定特定顺序),您可以执行
css_id = self.crawl_link.domain.product_infos.first.id_tag
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.