I have 3 models: Topic, Post, Link.
class Topic < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
has_many :links
belongs_to :topic
end
class Link < ActiveRecord::Base
belongs_to :post
end
I'd like to have counter_cache for forums on Link model.
How can I do this ?
Check out the counter_culture gem.
From the readme:
class Product < ActiveRecord::Base
belongs_to :sub_category
counter_culture [:sub_category, :category]
end
class SubCategory < ActiveRecord::Base
has_many :products
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :sub_categories
end
In the above example, the Category model will keep an up-to-date counter-cache in the products_count column of the categories table.
Related
I have following models in rails.
class User < ApplicationRecord
has_many :vendors
has_many :vendoritems, through: :vendors
has_many :products
end
class Vendorcode < ApplicationRecord
has_many :vendoritems
end
class Vendoritem < ApplicationRecord
belongs_to :vendorcode
belongs_to :vendor
end
class Vendor < ApplicationRecord
belongs_to :user
has_many :vendoritems
end
class Product < ApplicationRecord
belongs_to :user
belongs_to :vendorcode
has_many :vendoritems, XXXXX
end
Product has many vendoritems through vendorcode and user.
How can I implement this association.
I'd just go for an instance method like so
class Product < ApplicationRecord
belongs_to :user
belongs_to :vendorcode
def vendoritems
user.vendoritems
end
end
Cheers!
class Product < ApplicationRecord
belongs_to :user
belongs_to :vendorcode
def vendoritems
user.vendoritems.where('vendorcode =?', vendorcode.id)
end
end
I solved the problem.
Here is my models:
class User < ActiveRecord::Base
has_many :products
has_many :comments
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
I need to get comment records from current user products only
How do I do that? thanks
If we move the relationships to use a has_many: comments, through: products you can probably get what you're after:
class User < ActiveRecord::Base
has_many :products
has_many :comments, through: products
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
Now you can do user.comments.
The rails docs are here, which say:
A has_many :through association is often used to set up a many-to-many
connection with another model. This association indicates that the
declaring model can be matched with zero or more instances of another
model by proceeding through a third model. For example, consider a
medical practice where patients make appointments to see physicians.
My application has categories,than subcategories,than gigs
I have gigs_controller.rb in my controller file
And category.rb, subcategory.rb, gig.rb in my Model file.
How do i make their connection with each other,following the principle of category=subcategory=gig(by gig i mean i lot of small adds)
Will it be right to say in Category.rb model
class Category < ActiveRecord::Base
has_many :subcategories
end
in my Subcategory Model
class Subcategory < ActiveRecord::Base
belongs_to :category
end
and in my Gig.rb
class Gig < ActiveRecord::Base
belongs_to :user
belongs_to :category # I also need here to be belongs_to :subcategory,how do i do that?
end
Should i create a controller for category and subcategory ?, how would you go about that.
Thank you,for your support,hope my question will help others as well.
class Category < ActiveRecord::Base
has_many :subcategories
end
class Subcategory < ActiveRecord::Base
belongs_to :category
has_many :gigs
end
class Gig < ActiveRecord::Base
belongs_to :user
belongs_to :subcategory
end
Yes you can use has_many in model without having a controller.Because has_many uses table's name.
class Gig < ActiveRecord::Base
belongs_to :user
belongs_to :subcategory
end
class Category < ActiveRecord::Base
has_many :subcategories
end
class Subcategory < ActiveRecord::Base
belongs_to :category
has_many :gigs
end
I know sty must be wrong in the way I build my db but please take a minute answering this : I am building a supermarket model, where 1 user has a shopping list, each list has many products.
So what I do is :
class User < ActiveRecord::Base
has_many :lists
end
class List < ActiveRecord::Base
belongs_to :user
has_many :products
end
class Product < ActiveRecord::Base
????
end
A list has several products but products don't belong to lists. What should I do to have Users having many lists, and lists having many products ?
regards,
Have a class that links them via has_many through.
class ListItem < ActiveRecord::Base
belongs_to :list
belongs_to :product
end
class List < ActiveRecord::Base
belongs_to :user
has_many :list_items
has_many :products, through: :list_items
end
You don't need an additional class. Rails can manage this for you with the has_and_belongs_to_many_association
In your case, it would be:
class User < ActiveRecord::Base
has_many :lists
end
class List < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :lists
end
Of course, you need to add the join table in the migration:
create_table :lists_products, id: false do |t|
t.belongs_to :list
t.belongs_to :product
end
I have a model Article and model User. In users, there will be one creator of the article and many readers. How do I link these together?
I was thinking:
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
has_one created_by, through: :user (????)
end
class User < ActiveRecord::Base
has_many :articles
end
You can do like this
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
has_one created_by_user,:class_name => 'User'
end
If you want to specify a custom foreign_key(which is useful in these cases),you can specify a foreign_key option
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
has_one created_by_user,:class_name => 'User',:foreign_key =>'your_custom_fkey'
end