-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Models based on PostgreSQL views report 'nil' attribute #16555
Comments
I can confirm the issue on Used this reproduction |
Upon further investigation I found the cause of this behavior. It's because the model was not able to detect it's primary key. This leads to the You can circumvent the problem by doing: class ExampleView < ActiveRecord::Base
self.primary_key = "id"
end Given that there is an easy workaround and the issue being fixed on /cc @rafaelfranca |
I actually encountered this problem in a view without primary key - it was providing 'flattened' representation of another table: CREATE OR REPLACE VIEW item_materials_unnest AS
SELECT id AS item_id,
unnest(akeys(manufacturing_materials))::int AS material_id,
unnest(avals(manufacturing_materials))::int AS material_count
FROM items Given item with My workaround was adding def attribute_names
super.compact
end to the model. Given that a workaround exists, I'm okay with waiting for next release. |
@tmikoss I've added test-cases to ensure this will keep working in future versions. Thank you for reporting. |
Thanks! |
When trying to serialize a ActiveRecord model based on a PostgreSQL view rather than a table, I got a
TypeError (nil is not a symbol)
exception.The cause seems to be ActiveRecord reporting additional
nil
attribute for the model. For example, given a viewI get this
Bug observed with Rails 4.1.4 / 4.1.5, PostgreSQL 9.3.5. I've created a minimal Rails application with failing test cases at https://github.com/tmikoss/nil-attribute-in-views
The text was updated successfully, but these errors were encountered: