Associations don't inherit #9

Closed
RichardStokes opened this Issue Feb 15, 2012 · 10 comments

5 participants

@RichardStokes

Followed the guide in the readme, and have Pen inheriting from Product fine. However, your readme says that Pen should inherit associations. I have a User model with a has_many :products relationship, however I do not get a User.pens method?

@RichardStokes

Also dynamic attribute-based finders don't work for inherited attributes. For instance, Product.find_by_name(string) works, but Pen.find_by_name(string) does not (assuming Pen inherits an attribute 'name' from Product)

@hzamani
Owner

Pen inherit associations defined in Product model. For example the belongs_to :store, not the other side of it has_many :products (at least for now!)

Dynamic attribute-based finders will be added soon.
For now Product.find_by_name("some name").specific is a replacement!

@cscairns

yes, i could use the dynamic attribute finders myself

@vsanta

I'm having a similar problem. My superclass model, Content, has a belongs_to :site. Then Page inherits from Content. If I try to get all pages for a site in Page.where :site => site I get a SQL error saying pages.site does not exist. Did I misconfigure something?

@hzamani
Owner

@vsanta you should use :include option on acts_as

There are no dynamic attributed finder yet, and submodels inherent associations defined in supermodel not opposite ones, but may be added in feature!

@vsanta

How does this :include options work? Is it a hash set to true? Sorry but I did not find any samples or docs on what it does and how to use it.

Another problem I'm having is when trying to set an object that is associated with the parent class. Ex: Content is superclass to Page. Content belongs to Site. If I try to have something like page.site = some_site, it doesn't store the property.

@hzamani
Owner

@vsanta can you paste your models and migrations?

@bigardone

Hi!
I have the same problem.
I have a Company that has Clients, which can be Sellers or Buyers..

#Models
class Company < ActiveRecord::Base
  attr_accessible :name, :short_name
  has_many :clients
  has_many :sellers
  has_many :buyers
  validates :name, presence: true
  validates :short_name, presence: true
end

class Client < ActiveRecord::Base
  acts_as_superclass
  belongs_to :company, dependent: :destroy
  attr_accessible :comments, :name, :reference, :tic
  validates :reference, :name, :company_id, presence: true
end

class Buyer < ActiveRecord::Base
  acts_as :client, include: true
end

class Seller < ActiveRecord::Base
  acts_as :client, include: true
end

In my controller I try to get the sellers doing this:

#Controller
@sellers = current_user.company.sellers

And this is the error I get:

#Error
SQLite3::SQLException: no such column: sellers.company_id: SELECT "sellers".* FROM "sellers" INNER JOIN "clients"     ON "clients"."as_client_id" = "sellers"."id" AND "clients"."as_client_type" = 'Seller' WHERE "sellers"."company_id" = 1

I'm really newbie to Rails, so maybe there's something I'm missing... any ideas?

Thanks in advance!

@hzamani
Owner

Sorry for late response!

Remove has_many :sellers and has_many :buyers from Company and define these functions:

def sellers() clients.where as_client_type: "Seller" end
def buyers()  clients.where as_client_type: "Buyer"  end

now current_user.company.sellers will do what you expect

@bigardone

Hi @hzamani !
Now it works like a charm!
Thank you very much

@hzamani hzamani closed this Jul 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment