Hi i have a problem with "attr_accessor" in rails 4
I have a model with many associations and when I use attr_accessor I put the field_name but with my association i have many table with the same field name.
For exemple
class User < ActiveRecord::Base
has_one :agent
has_one :language
attr_accessor :code
end
But i have a field :code in agent table and in language table. I'm trying to find a solution on internet but without success
Is there a way to specify the table name ?
You can use these way to get model speicific code from user model
class User < ActiveRecord::Base
has_one :agent
has_one :language
delegate :code, to: :agent, prefix: true, allow_nil: true
delegate :code, to: :language, prefix: true, allow_nil: true
end
As an example:
Now you can access it User.first.agent_code for agent model Also you can access it User.first.language_code for language model
You can access the specific code by specific model wise
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With