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
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.
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.
I have these models:
class Car < ActiveRecord::Base
has_many :car_services
end
class CarService < ActiveRecord::Base
belongs_to :car
belongs_to :car_service_definition
end
class CarServiceDefinition < ActiveRecord::Base
has_many :car_services
end
I am trying to find out if the currently selected car has a certain service - trying to do it this way:
airbag = car.car_services.car_service_definitions.where('service_type = "Airbag"').first
But this query doesn't work because of the wrong using model associations.
How do I find out, if the current car has some airbags?
Thank you in advance.
Assuming your migrations are fine
class Car < ActiveRecord::Base
has_many :car_services
end
class CarService < ActiveRecord::Base
belongs_to :car
belongs_to :car_service_list
has_and_belongs_to_many :car_service_definitions
end
class CarServiceDefinition < ActiveRecord::Base
end
airbag = car.car_services.car_service_definitions.where(service_type: 'AirBag').first
Well, from the look of the relationships, I assume that car_services is the rich join table of cars and car_service_definitions
What you can do is to set up has_many :through relationship on both car and car_service_definition
class Car < ActiveRecord::Base
has_many :car_services
has_many :car_service_definitions, through: :car_services
end
class CarService < ActiveRecord::Base
belongs_to :car
belongs_to :car_service_definition
end
class CarServiceDefinition < ActiveRecord::Base
has_many :car_services
has_many :cars, through: :car_services
end
And then if you want to find airbag, it would be like this
airbag = car.car_service_definitions.where("car_service_definitions.service_type" => 'AirBag').first
But if you want to check if the car has air_bag, could just write a method like this
class Car < ActiveRecord::Base
def has_air_bag?
car_service_definitions.where("car_service_definitions.service_type" => 'AirBag').count > 0
end
end
how can I get a weight of product through a Record model? As I know, possible to get all products of certain record but I cannot find out the way getting the weight of certain product.
class User < ActiveRecord::Base
has_many :eatings
end
class Eating < ActiveRecord::Base
belongs_to :user
has_many :records
end
class Record < ActiveRecord::Base
belongs_to :eating
end
class Product < ActiveRecord::Base
end
class WeightedProduct < ActiveRecord::Base
end
What relationships should have Record and Product models with WeightedProduct so user will be able to get weight of certain product through one line User.first.eatings.first.records.first.products.first.weight?
Looks like you're after this:
class Record < ActiveRecord::Base
belongs_to :eating
has_many :weighted_products
end
class Product < ActiveRecord::Base
has_many :weighted_products
end
class WeightedProduct < ActiveRecord::Base
belongs_to :record
belongs_to :product
end
Then User.first.eatings.first.records.first.weighted_products.first.weight
I think that should work but haven't tested.
it seems that each product has one weighted product, then in that case you should add
class Product < ActiveRecord::Base
has_one :weighted_product
end
class WeightedProduct < ActiveRecord::Base
belongs_to :product
end
and
class Record < ActiveRecord::Base
belongs_to :eating
has_many :products
end
I'm new to rails and working on an app that has the following situation:
Users have skills (e.g rafting, dancing)
Users participate in contests
Contest measures multiple skills
At the end of each contest, each user gets a score (e.g dancing: 5, rafting: 4)
Whats the best way to model this ?
Thanks,
This got nasty :s At the end I was actually not sure if this is the right way
class Skill < ActiveRecord::Base
has_many :skill_scores
has_many :user_skills
end
class UserSkill < ActiveRecord::Base
belongs_to :user
belongs_to :skill
end
class SkillScore < ActiveRecord::Base
belongs_to :user
belongs_to :contest
belongs_to :skill
end
class User < ActiveRecord::Base
has_many :skills
has_many :contests, :through => :contest_participations
has_many :skill_scores
end
class Contest < ActiveRecord::Base
has_many :users, :through => :contest_participations
has_many :skill_scores
end
class ContestParticipation < ActiveRecord::Base
belongs_to :user
belongs_to :contest
end