Rails机械化连接表循环

时间:2018-08-13 03:30:39

标签: ruby-on-rails mechanize jointable

我有一个具有Listing,Tag和Listing_Tag模型的应用。我想创建标签并在机械化任务中分配它们。目前,我正在创建清单,遍历标签并创建它们,但是当我开始通过清单标签将标签分配给清单时,会出现此错误

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>'

第170行是page.search,第181行是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

在“机械化”中,我创建一个列表和标签。

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

然后,我想通过Listing_tag关联列表和标签

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

我在做什么错?是我的联接表设置正确(我认为这是因为它可以以这种形式工作)

1 个答案:

答案 0 :(得分:1)

您似乎想执行以下操作:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix dist2mat(NumericVector& x, int bf) {

  /* input validation */
  if (!x.inherits("dist")) stop("Input must be a 'dist' object");
  int n = x.attr("Size");
  if (n > 65536) stop("R cannot create a square matrix larger than 65536 x 65536");

  /* initialization */
  NumericMatrix A(n, n);

  /* use pointers */
  size_t j, i, jj, ni, nj; double *ptr_x = &x[0];
  double *A_jj, *A_ij, *A_ji, *col, *row, *end;

  /* fill in lower triangular part */
  for (j = 0; j < n; j++) {
    col = &A(j + 1, j); end = &A(n, j);
    while (col < end) *col++ = *ptr_x++;
    }

  /* cache blocking factor */
  size_t b = (size_t)bf;

  /* copy lower triangular to upper triangular; cache blocking applied */
  for (j = 0; j < n; j += b) {
    nj = n - j; if (nj > b) nj = b;
    /* diagonal block has size nj x nj */
    A_jj = &A(j, j);
    for (jj = nj - 1; jj > 0; jj--, A_jj += n + 1) {
      /* copy a column segment to a row segment */
      col = A_jj + 1; row = A_jj + n;
      for (end = col + jj; col < end; col++, row += n) *row = *col;
      }
    /* off-diagonal blocks */
    for (i = j + nj; i < n; i += b) {
      ni = n - i; if (ni > b) ni = b;
      /* off-diagonal block has size ni x nj */
      A_ij = &A(i, j); A_ji = &A(j, i);
      for (jj = 0; jj < nj; jj++) {
        /* copy a column segment to a row segment */
        col = A_ij + jj * n; row = A_ji + jj;
        for (end = col + ni; col < end; col++, row += n) *row = *col;
        }
      }
    }

  /* add row names and column names */
  A.attr("dimnames") = List::create(x.attr("Labels"), x.attr("Labels"));

  return A;
  }