2

I'm trying to create the simplest has_many relationship possible for one of my models. It's defined like that:

# i know it doesn't make much sense. I'm using such ridiculous 
# where case to keep things simple for now
has_many :jobs, -> { where(id: 1) }, class_name: SidekiqJob

However, when i' trying to call that relationship in anyway, for example with MyModel.last.jobs, rails throws:

NoMethodError: undefined method `name' for nil:NilClass
from /Volumes/HDD/Users/michal/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.3/lib/active_record/relation/merger.rb:141:in `block in filter_binds'

Has anyone have any idea on what is going wrong in here?

  • ruby 2.1.1
  • rails 4.0.3

edit:

Original association definition:

has_many :jobs, (obj) -> { where('jid LIKE ?', "#{obj.superjob_id}%") }, class_name: SidekiqJob
mbajur
  • 4,406
  • 5
  • 49
  • 79
  • Where is `name` being called? I don't see it in the code you supplied, just in the error. – Wes Foster Jul 04 '16 at 15:45
  • 2
    I think has_many only works with objects and not model. jobs method shall work on objects of your model – Vijay Meena Jul 04 '16 at 15:46
  • @WesFoster it's not called anywhere. At least not by my code. – mbajur Jul 04 '16 at 15:46
  • @VijayMeena you're obviously right, that was a mistake, typo fixed – mbajur Jul 04 '16 at 15:48
  • 1
    Have you tried different column names inside where, and not id - will it work? What is the purpose of your condition for id here (I got it's simple stub, but what is your goal)? Just in case, relation like `has_many` has option such as `foreign_key` to specify column name of the child table to use for relation. – Pavel Bulanov Jul 04 '16 at 17:56
  • 1
    The reason because I ask is that I opened Rails source code at the line that you gave - [here](https://github.com/rails/rails/blob/v4.0.3/activerecord/lib/active_record/relation/merger.rb#L141). The call is happening from [this](https://github.com/rails/rails/blob/v4.0.3/activerecord/lib/active_record/relation/merger.rb#L107) method. It might be that your where clause somehow gets eliminated (or eliminates) the where clause that joins two tables (by id column). – Pavel Bulanov Jul 04 '16 at 17:58
  • @PavelBulanov i've edited my OP, added original where statement – mbajur Jul 04 '16 at 21:36
  • Just in case, it seems that syntax for lamba is different (order of -> and obj), see [here](http://stackoverflow.com/questions/2462203/rails-has-many-with-dynamic-conditions) - should be `has_many :jobs, -> (obj) { ...}` – Pavel Bulanov Jul 05 '16 at 08:17

2 Answers2

1
has_many :jobs, -> { where(id: 1) }, class_name: SidekiqJob

Without digging into the source to see if something like to_s is called on the class_name value, it appears the syntax is incorrect and would require quotation marks around the class name:

has_many :jobs, -> { where(id: 1) }, class_name: "SidekiqJob"

See RailsGuides here: http://guides.rubyonrails.org/association_basics.html#scopes-for-has-many-where

class Author < ApplicationRecord
  has_many :confirmed_books, -> { where "confirmed = 1" },
    class_name: "Book"
end

From 3_2_release_notes.md: https://github.com/rails/rails/blob/37b36842330b5db1996fda80e387eae3a5781db8/guides/source/3_2_release_notes.md

Allow the :class_name option for associations to take a symbol in addition to a string. This is to avoid confusing newbies, and to be consistent with the fact that other options like :foreign_key already allow a symbol or a string.

has_many :clients, :class_name => :Client # Note that the symbol need to be capitalized
SoAwesomeMan
  • 3,226
  • 1
  • 22
  • 25
0

It turned out to be related to ruby/active_record versions. According to this thread: create with has_many through association gets NoMethodError (undefined method `name' for nil:NilClass)

What i've done to "fix" that was to change my ruby version to 2.1.10. Then, i got rid of such errors (cause they've been thrown in more places). Anyway, i am still not able to includes my relation defined as in the OP. It seems that it's not possible to includes relations using custom where statements.

Community
  • 1
  • 1
mbajur
  • 4,406
  • 5
  • 49
  • 79