简体   繁体   English

ActiveRecord_Associations_CollectionProxy的未定义方法'id_tag'

[英]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.

相关问题 Rails“ActiveRecord_Associations_CollectionProxy的未定义方法” - Rails “undefined method for ActiveRecord_Associations_CollectionProxy” ActiveRecord_Associations_CollectionProxy的未定义方法[rails] - undefined method for ActiveRecord_Associations_CollectionProxy [rails] 如何修复“未定义的方法‘用户’(ActiveRecord_Associations_CollectionProxy) - How to fix "undefined method `user' (ActiveRecord_Associations_CollectionProxy) 创建提要=&gt; NoMethodError:ActiveRecord_Associations_CollectionProxy的未定义方法 - Creating a feed => NoMethodError: undefined method for ActiveRecord_Associations_CollectionProxy #的未定义方法“引荐” - undefined method `referrals' for #<User::ActiveRecord_Associations_CollectionProxy: #的未定义方法&#39;id&#39; <Page::ActiveRecord_Associations_CollectionProxy:0x007f4723af2208> - undefined method 'id' for #<Page::ActiveRecord_Associations_CollectionProxy:0x007f4723af2208> #的未定义方法`count =&#39; - undefined method `count=' for #<User::ActiveRecord_Associations_CollectionProxy ActiveRecord_Associations_CollectionProxy上的重写方法 - Override method on ActiveRecord_Associations_CollectionProxy ActiveRecord_Associations_CollectionProxy问题 - ActiveRecord_Associations_CollectionProxy issue Rails,ActiveRecord_Associations_CollectionProxy调用ActiveRecord方法的方式 - Rails, The way for ActiveRecord_Associations_CollectionProxy to call method of ActiveRecord
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM