J
J
justed_ss2015-05-21 07:23:36
Ruby on Rails
justed_ss, 2015-05-21 07:23:36

How to write a joins query in rails for polymorphic links?

There are models

class Entry< ActiveRecord::Base
  belongs_to :record, polymorphic: true
end

class EntrySpecial< ActiveRecord::Base
  has_one :entry, as: :record
end

I just can't compose a joins query that would select those Entry that satisfy certain conditions, refer to EntrySpecial, and whose EntrySpecial has certain properties
Ruby 2.0 Rails 414

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
justed_ss, 2015-05-21
@justed_ss

solved the problem a bit

class Entry< ActiveRecord::Base
  belongs_to :record, polymorphic: true
  belongs_to :entry_special, foregin_key: 'record_id'
end

After that, it became possible to compose a "classic" join via activerecord.
But it looks like a crutch.

I
Ivan Nesterovich, 2015-05-21
@vanderv

I recommend using gem squeel. It's
done like this: Entry.joins { record(EntrySpecial) }
And then you can include selection conditions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question