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

has_one with sub class does not work #9

Closed
kidlab opened this issue Aug 17, 2014 · 17 comments
Closed

has_one with sub class does not work #9

kidlab opened this issue Aug 17, 2014 · 17 comments

Comments

@kidlab
Copy link

kidlab commented Aug 17, 2014

Given these models:

class Photo < ActiveRecord::Base
  actable
  belongs_to :user
end

class Avatar < ActiveRecord::Base
  acts_as :photo
end

class User < ActiveRecord::Base
  has_one :avatar
  has_many :photos
end

When I call user.avatar it raises an error like this:

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column avatars.user_id does not exist

It seems that we haven't supported this feature yet. Is there anyway we can work around this?
Thanks.

@hzamani
Copy link
Owner

hzamani commented Aug 17, 2014

Your example should work. What version of rails you are using? What is your database schema?

@igrek8
Copy link

igrek8 commented Aug 17, 2014

I have the same problem #10

@kidlab
Copy link
Author

kidlab commented Aug 18, 2014

I'm using Rails 4.1.4. Here is the brief DB schema:

# Photo
create_table "photos", id: :uuid, default: "uuid_generate_v4()", force: true do |t|
  t.uuid     "user_id"
  t.uuid     "actable_id"
  t.string   "actable_type"
  t.timestamps
end

# Avatar
create_table :avatars, id: :uuid, default: "uuid_generate_v4()", force: true do |t|
  t.boolean :is_profile_avatar
  t.timestamps
end

It's obvious that it would work if I add a column user_id to avatars table, but it seems not very correct to me. So, what is the correct way to define DB schema to work with active_record-acts_as?

By the way, it would be clearer if you include the sample DB schema in the README file, as I'm very confused about how the schema looks like when using acts_as. Our goal is to avoid empty columns in the master table, and sub-tables should not duplicate columns from the master one, is it correct?

@igrek8
Copy link

igrek8 commented Aug 18, 2014

@kidlab did you find anything useful to solve your problem? I really need it urgently...

With your example, you can try this, but it is not exactly what you are looking

# User.rb
has_many :photos
has_one :avatar, through: :photos, source: :actable, source_type: 'Avatar'

@kidlab
Copy link
Author

kidlab commented Aug 18, 2014

@se-meridian Thank you for the suggestion. I spent sometimes to walk through the source code of the gem, and I haven't found anything useful yet. It seems to me that it does not support advance feature like that.

@igrek8
Copy link

igrek8 commented Aug 19, 2014

@kidlab, you mentioned about adding column user_id to your avatar table, since I have the same issue as yours, I decided to make super class to be abstract and just specified belongs_to there without including the column definition and only specified it for sub classes. So you can make Photo Photable and extend it with Photo and Avatar with user_id, so you will be able to work with sub classes... however, it is just a workaround but not a solution for MTI

@igrek8
Copy link

igrek8 commented Aug 22, 2014

I had been waiting for the reply, however, I had better switch to concerns to solve my problems -_-

@hzamani
Copy link
Owner

hzamani commented Aug 22, 2014

It is fully working with sqlite on my specs, there might be a problem with pg or eager loading. I will test it on my first free time.

@igrek8
Copy link

igrek8 commented Aug 23, 2014

@hzamani, can you upload your working example with has_many childs?

@maurobender
Copy link

I'm having the same problem as @kidlab. When defining associations to sub models the generated where condition seems to be wrong because is using the sub model table name to reference fields that belongs to the parent model, which leads to a db query error.

Here is how I'm modeling part of my application:

class MoneyTransaction < ActiveRecord::Base
  actable
end

class AirtimeTransaction < ActiveRecord::Base
  acts_as :money_transaction
end

class User < ActiveRecord::Base
  has_many :airtime_transactions
end

The problem comes when I ask for the user's airtime transactions, it fails with this sql error:

2.1.2 :001 > User.find(1).airtime_transactions
  User Load (0.8ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  SQL (0.3ms)  SELECT "airtime_transactions"."id" AS t0_r0, (...), "money_transactions"."id" AS t1_r0, "money_transactions"."user_id" AS t1_r1, (...), "money_transactions"."actable_id" AS t1_r5, "money_transactions"."actable_type" AS t1_r6, (...) FROM "airtime_transactions" LEFT OUTER JOIN "money_transactions" ON "money_transactions"."actable_id" = "airtime_transactions"."id" AND "money_transactions"."actable_type" = 'AirtimeTransaction' WHERE "airtime_transactions"."user_id" = ?  [[nil, 1]]
  SQLite3::SQLException: no such column: airtime_transactions.user_id.

I tried to debug the Gem's code but I couldn't find where the problem is. Maybe with this extra information you can now figure out where the issue comes from.

Thanks.

@abacha
Copy link

abacha commented Oct 9, 2014

I don't know if its the same problem:

class Asset < ActiveRecord::Base
  actable
  has_many :balances
end
class Fund < ActiveRecord::Base
  act_as :asset
end
[12] pry(main)> Fund.last.balances
Mysql2::Error: Unknown column 'balances.fund_id' in 'where clause': SELECT `balances`.* FROM `balances`  WHERE `balances`.`fund_id` = 1104

@abacha
Copy link

abacha commented Oct 9, 2014

if I put has_many :balances, through: :asset on Fund, it works
but isn't it supposed to inherit associations?

@hachpai
Copy link

hachpai commented Jan 26, 2016

I've the same issue. The query doesn't inherit correctly of the acting_as model, so column name is wrong.

@marat-y
Copy link

marat-y commented Nov 1, 2016

same problem

@manuelmeurer
Copy link
Collaborator

@hachpai @icemall Could you post your code and db schema?

@manuelmeurer
Copy link
Collaborator

This problem is probably fixed in v2.0.3 which was just released. Please open another issue if it still happens with that version.

@amrdruid
Copy link

Works fine in the new release

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

9 participants