I have following models in a Rails application.
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
class Client < ActiveRecord::Base
has_one :address, :as => :addressable, dependent: :destroy
accepts_nested_attributes_for :address, :allow_destroy => true
has_many :invoices
end
class Invoice < ActiveRecord::Base
belongs_to :client
end
While I am able to retrieve the client name using
#invoice.client.name
I am not able to retrieve the address information in the similar manner.
How do I retrieve the address attributes in the view for invoice?
#invoice.client.address that is the aswer. But i recommend you follow the Law of Demeter using the method delegate
http://en.wikipedia.org/wiki/Law_of_Demeter
http://api.rubyonrails.org/classes/Module.html#method-i-delegate
Basically the idea is that you can do this: #invoice.client.address_street o better #invoice.client_address_street
Related
As i mentioned on the question..
I know this error and i know how associations work
In my case i have ProductRepository model which belongs_to :category
So of course when i'm trying to delete this category which this ProductRepository object belongs to i should get this error and it's totally fine and i know how to handle this exception
Now let's say that i have a many models which referring to categories like
Product, ProductRepositoy, Post, etc...
My question here, how can i get a list of this objects which belongs to categories, or even get a class name array or whatever
So when i handle this exception i can send a message to ask the user to check and delete the following before deleting this category ?
Example:
Once user delete category... if it has any items which belongs to it
he should get a message saying Please check the following (Posts, Products, etc..)
For who is going to answer, all i need is how to get this objects or even class names only
And i can do the rest
Thanks in advance :)
Here is the file, after removing what isn't necessary for the question
category.rb
class Category < ActiveRecord::Base
has_many :brands, :dependent => :destroy
has_many :products, :dependent => :destroy
end
product_respository.rb
class ProductRepository < ActiveRecord::Base
belongs_to :category
has_many :unity_folders, as: :product, dependent: :destroy
validates :name, :price, :barcode, presence: true
validates :barcode, uniqueness: true
end
product.rb
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :brand
belongs_to :organization
has_many :unity_folders, as: :product, dependent: :destroy
has_one :product3d
belongs_to :shop
end
I'm using a has many through pattern with these 3 models
class User < ActiveRecord::Base
has_many :user_topics
has_many :topics, through: :user_topics
end
class Topic < ActiveRecord::Base
validates_presence_of :name
validates :name, :uniqueness => true
end
class UserTopic < ActiveRecord::Base
belongs_to :user
belongs_to :topic
accepts_nested_attributes_for :topic
end
At the moment a new topic model is trying to be created every time a new user_topic is created. I'd like to create a new topic model only if the topic name doesn't already exist, otherwise if it does, use the existing topic_id.
So something like:
class UserTopic < ActiveRecord::Base
belongs_to :user
belongs_to :topic
accepts_nested_attributes_for :topic, :first_or_create(:name)
end
Is it possible to do something similar to this?
I have two models that both accept_nested_attributes_for each other. I know this is causing issues with rails_admin but can't seem to figure out the appropriate command to exclude nested attributes in my configuration. Ideally I would like to exclude nested attributes for all models so that this problem does not happen again.
Branden,
You need to inform the bi-direction association using inverse_of:
Ex:
class User < ActiveRecord::Base
belongs_to :company, :inverse_of => :users
accepts_nested_attributes_for :company
end
For both sides of association:
class Company < ActiveRecord::Base
has_many :users, inverse_of: :company
accepts_nested_attributes_for :users, :allow_destroy => true
end
More information in the docs: http://guides.rubyonrails.org/association_basics.html#bi-directional-associations
I have a fairly normal class structure, using a polymorphic association:
class Contact < ActiveRecord::Base
has_many :opportunities, :as => :has_opportunities, dependent: :destroy
end
class Company < ActiveRecord::Base
has_many :opportunities, :as => :has_opportunities, dependent: :destroy
end
class Opportunity < ActiveRecord::Base
belongs_to :has_opportunities, polymorphic: true
belongs_to :contact, foreign_key: 'has_opportunities_id', conditions: "opportunities.has_opportunities_type = 'Contact'"
belongs_to :company, foreign_key: 'has_opportunities_id', conditions: "opportunities.has_opportunities_type = 'Company'"
end
In Rails 4 using :conditions has been deprecated, but I can't figure out the "new" syntax required to allow access to the parent object from the child.
Edit: Yes, you can do opportunity.has_opportunities which will return you a Contact or a Company, but it is often "nicer" in code to use opportunity.contact or opportunity.company
Can't you simply set it up as a regular polymorphic association?
class Opportunity < ActiveRecord::Base
belongs_to :has_opportunities, polymorphic: true
end
I have three models as follows:
class User < ActiveRecord::Base
...
has_many :feeds
...
end
class Project < ActiceRecord::Base
...
has_many :feeds
has_many :users, through: :feeds
...
end
class Feed < ActiveRecord::Base
...
belongs_to :user
belongs_to :project
...
end
I want to model the situation where a user can have a maximum of one feed per project. I know that I can do this check in a custom validator within the Feed class, but is there a way to model this using only ActiveRecord associations?
You can do that on Feed.rb:
validates :user_id, :uniqueness => {:scope => :project_id}