softace / activerecord-tableless

Bringing together the different tableless model implementations into a single gem/plugin
Other
113 stars 41 forks source link

Does the gem supports initialization of tableless model instance through association? #17

Open jibiel opened 10 years ago

jibiel commented 10 years ago

Greetings.

Currently I'm trying to initialize object through has_one association like this:

class Report < ActiveRecord::Base
  has_no_table

  belongs_to :reportable, polymorphic: true

  column :reportable_id, :integer
  column :reportable_type, :string
  column :period, :daterange, 1.month.ago..Time.current

  attr_accessible :smth
end
class Store < ActiveRecord::Base
  has_one :report, as: :reportable

  # ...
end

But getting only:

> Store.take.build_report
  Store Load (0.6ms)  SELECT "stores".* FROM "stores" LIMIT 1
NoMethodError: undefined method `sanitize_limit' for #<Object:0x000001059b8ba0>
from /Users/jibiel/.rvm/gems/ruby-2.0.0-p451@smth/gems/activerecord-4.0.0/lib/active_record/relation/query_methods.rb:808:in `build_arel'

Is this currently supported? Could you please provide an example?

I suspect this is rather me not understanding things than lack of implementation. Anyways, it'll be great to see such example in the README.md.

Thanks.

jarl-dk commented 10 years ago

I'll take a look at it...

simonweil commented 10 years ago

This interests me too, did you find anything?

jarl-dk commented 10 years ago

Sorry guys... I have been busy. I'll see if I can find some time... However at first glance I see that your parent model Store has a table, whereas your child model Report does not have a table. Can you try this with the parent model also being tableless, please?

jibiel commented 10 years ago

@jarl-dk Ok, here's the new model:

class Reportable < ActiveRecord::Base
  has_no_table

  has_one :report, as: :reportable

  column :smth, :string
end

The other two are still the same (though I've added polymorphic association, just ignore that).

Here's what we have:

> Store.take.build_report # Store «has_table»
  Store Load (0.6ms)  SELECT "stores".* FROM "stores" LIMIT 1
NoMethodError: undefined method `sanitize_limit' for #<Object:0x007fd5b9838560>
from /Users/jibiel/.rvm/gems/ruby-2.0.0-p481@smth/gems/activerecord-4.0.0/lib/active_record/relation/query_methods.rb:808:in `build_arel'
> Store.new.build_report # Store «has_table»
=> #<Report reportable_id: nil, reportable_type: "Store", period: Sun, 01 Jun 2014 21:18:39 MSK +04:00..Tue, 01 Jul 2014 21:18:39 MSK +04:00>
> Reportable.take # Reportable has_no_table
NoMethodError: undefined method `sanitize_limit' for #<Object:0x007fd5b98f0ed0>
from /Users/jibiel/.rvm/gems/ruby-2.0.0-p481@smth/gems/activerecord-4.0.0/lib/active_record/relation/query_methods.rb:808:in `build_arel'
> Reportable.new.build_report # Reportable has_no_table
=> #<Report reportable_id: nil, reportable_type: "Reportable", period: Sun, 01 Jun 2014 21:18:39 MSK +04:00..Tue, 01 Jul 2014 21:18:39 MSK +04:00>

That is nothing new. I mean, I can't take tableless object, right?

jibiel commented 10 years ago

I suppose, that is not a big deal if I'm the only one concerning. I imagine that the concept is difficult enough without the thing I'm asking.

Meanwhile, we use simple workaround at the office:

class Store < ActiveRecord::Base
  # ...
  def report
    Report.new reportable: self
  end
end
> Store.take.report  # Store «has_table»
  Store Load (0.6ms)  SELECT "stores".* FROM "stores" LIMIT 1
=> #<Report reportable_id: 321, reportable_type: "Store", period: Sun, 01 Jun 2014 21:31:35 MSK +04:00..Tue, 01 Jul 2014 21:31:35 MSK +04:00>
t-anjan commented 9 years ago

I have created a PR #24 which fixes this issue for me. Not heavily tested, though.

jibiel commented 8 years ago

@jarl-dk ActiveRecord::Tableless is a fine gem. I personally use it for every rails project when migrating from other frameworks, like CodeIgniter, WordPress, etc. Have you thought about assigning more Collaborators / Maintainers? You could pick someone from the forks.

pboling commented 6 years ago

@jarl-dk I find myself reaching for this gem on most projects as well. Please ask for help maintaining if you are no longer able to do it effectively.

I also need to understand how to build associations to has_no_table models properly.