Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jibiel opened this issue May 21, 2014 · 8 comments

Comments

@jibiel
Copy link

jibiel commented May 21, 2014

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
Copy link
Member

jarl-dk commented May 22, 2014

I'll take a look at it...

@simonweil
Copy link

This interests me too, did you find anything?

@jarl-dk
Copy link
Member

jarl-dk commented Jun 30, 2014

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
Copy link
Author

jibiel commented Jul 1, 2014

@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
Copy link
Author

jibiel commented Jul 1, 2014

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 added a commit to t-anjan/activerecord-tableless that referenced this issue Nov 6, 2014
…ough an association, it was throwing a 'NoMethodError: undefined method `sanitize_limit' for #<Object:...>".

Fixed the error by defining that method on the "connection" variable. Took the method from the ActiveRecord gem: /lib/active_record/connection_adapters/abstract/database_statements.rb .
@t-anjan
Copy link

t-anjan commented Nov 6, 2014

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

clemcke pushed a commit to clemcke/activerecord-tableless that referenced this issue Apr 23, 2015
…ough an association, it was throwing a 'NoMethodError: undefined method `sanitize_limit' for #<Object:...>".

Fixed the error by defining that method on the "connection" variable. Took the method from the ActiveRecord gem: /lib/active_record/connection_adapters/abstract/database_statements.rb .
Chocksy added a commit to EpicCoders/activerecord-tableless that referenced this issue Jun 17, 2015
@jibiel
Copy link
Author

jibiel commented Oct 26, 2015

@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
Copy link

pboling commented Nov 8, 2017

@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.

pboling referenced this issue in boltthreads/activerecord-tablefree Nov 10, 2017
… association, it was throwing a 'NoMethodError: undefined method `sanitize_limit' for #<Object:...>".

Fixed the error by defining that method on the "connection" variable. Took the method from the ActiveRecord gem: /lib/active_record/connection_adapters/abstract/database_statements.rb .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants