Rails Mechanize Join Table Loop

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Rails Mechanize Join Table Loop



I have an app that has a Listing, Tag and Listing_Tag Model. I want to create tags and assign them in a mechanize task. At the moment I am creating the listing, looping through the tags and creating them but when I get to the assigning the tag to the listing through listing tag I get this error


rake aborted!
NoMethodError: undefined method `each' for 0:Integer
/Users/mycomp/RubymineProjects/OpportunityFinder/lib/tasks/brisbane.rake:181:in `block (2 levels) in <top (required)>'
/Users/mycomp/.rvm/gems/ruby-2.4.2/gems/nokogiri-1.8.4/lib/nokogiri/xml/node_set.rb:204:in `block in each'
/Users/mycomp/.rvm/gems/ruby-2.4.2/gems/nokogiri-1.8.4/lib/nokogiri/xml/node_set.rb:203:in `upto'
/Users/mycomp/.rvm/gems/ruby-2.4.2/gems/nokogiri-1.8.4/lib/nokogiri/xml/node_set.rb:203:in `each'
/Users/mycomp/RubymineProjects/OpportunityFinder/lib/tasks/brisbane.rake:170:in `block in <top (required)>'
/Users/mycomp/.rvm/gems/ruby-2.4.2@global/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/Users/mycomp/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in `eval'
/Users/mycomp/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in `<main>'



line 170 is page.search and 181 is for tag in t do


class Listing < ApplicationRecord

has_many :listing_tags
has_many :tags, through: :listing_tags

class ListingTag < ApplicationRecord
belongs_to :listing
belongs_to :tag
end

class Tag < ApplicationRecord
has_many :listings, through: :listing_tags
end



in Mechanize I create a Listing & Tags.


page.search('a[title="view business"]').each do |link|
mechanize.click(link)
l = Listing.find_or_create_by(name: mechanize.page.css('article h1[itemprop="name"]').text.strip)
t = mechanize.page.css('body main div.row.flex-page div.column.medium-3.__left-col-small article ul:nth-child(14) li').each do |tag|
Tag.find_or_create_by(name: tag.text.strip)
end



I then want to associate the listing and tag through the Listing_tag


for tag in t do
ListingTag.find_or_create_by(listing_id: l.id, tag_id: tag.id)
end



What am I doing wrong? is the my join table set up right( i think it is cause it works in the form)




1 Answer
1



It looks like you want to do something like:


ListingTag.find_or_create_by(listing_id: listing.id, tag_id: tag.id)





so I could do away with the l= & t= ?
– Joe Bloggs
Aug 13 at 5:47





doesnt it have to be done in a loop incase there are multiple tags?
– Joe Bloggs
Aug 13 at 6:03





Yes, you would put it in the same loop as Tag.find_or_create_by, right after it.
– pguardiario
Aug 13 at 6:20






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS