I am trying a polymorphic association
between tables. I have this in my User model.
user.rb
has_many :custom_field_data, as: :customizable
accepts_nested_attributes_for :customizable
custom_field_data.rb
class CustomFieldData < ActiveRecord::Base
belongs_to :customizable, polymorphic: true
belongs_to :custom_field
end
when I am trying to build @user
attribute in users_controller
like this:
def new
@user = @current_company.users.build
@customizable = @user.customizable.build
end
its throwing me an error:
NoMethodError (undefined method `customizable' for #<User:0x000000045dbe58>):
app/controllers/users_controller.rb:16:in `new'
when I am trying to access @user.custom_field_data
, I am getting this:
*** NameError Exception: uninitialized constant User::CustomFieldDatum
I don't know, where I am doing wrong. Thanks in advance.
if you are using gem files. once again bundle it
try this out:
this issue is because of you have wrong model name(plural) ie CustomFieldData, it should be CustomFieldDatum, because data is a plural word, and Datum is singular.
so to fix it specify class name along with association in user model.
has_many :custom_field_data, as: :customizable, class_name: 'CustomFieldData'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.