trying the rails model association - ruby-on-rails

I'm not sure if this a best practices in terms of association. Anyone can help me
//post,rb
class Post < ApplicationRecord
belongs_to :user
has_one :location, through: :user
has_one :category, through: :user
has_one :type, through: :user
end
//user.rb
class User < ApplicationRecord
has_many :posts
end
//category.rb
class Category < ApplicationRecord
belongs_to :post
end
//location.rb
class Location < ApplicationRecord
belongs_to :post
end
//type.rb
class Typo < ApplicationRecord
belongs_to :post
end
So the one of main goal of this are it's like
User.posts.location,create(country: "Japan", city: "Kyoto")
but i get an error with location NoMethodError: undefined method `location' for #
also should i a references in post like location:references type:references category:references

You need to rename the classes like
#location.rb
class Location< ApplicationRecord
belongs_to :post
end
#type.rb
class Type < ApplicationRecord
belongs_to :post
end
Not need to
through: :user #=> this use for many to many relationship
You can remove this

Related

how to use has many through with condition in rails

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.

rails has_many and has_one

I have two Models:
User:
class User < ActiveRecord::Base
has_many :comment
Comment:
class Comment < ActiveRecord::Base
belongs_to :movie
has_one :user
end
What I want now is:
that each comment is related exactly to One User
But each user can have mandy Comments ...
But when I want to store it to db I got this error:
Where is my mistake?
On your code:
class Comment < ActiveRecord::Base
belongs_to :movie
has_one :user
end
Try to change:
has_one :user
To:
belongs_to :user
And you can now use, #comment.user = #user.

Retrieve polymorphic associations in Rails from one model

class User < ActiveRecord::Base
has_many :posts
has_many :images, as: :imageable
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :images, as: :imageable
end
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
Is there a specific way where I can do User.images and get both the user's images and the post's images that belong to that user?
For some reason I can't wrap my head around how to do this best.
In that case you can avoid polymorphic relationship, simple has_many and belongs_to will suffice. where :
class User ActiveRecord::Base
has_many :posts
has_many :images
end
class Post ActiveRecord::Base
belongs_to :user
has_many :images
end
class Image < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
But then again I think you wanted to ask about User.last.images and not User.images
the same can be done through has_many through associations as well

How to find out user name

I have this setup:
class User < ActiveRecord::Base
has_many :posts, :dependent => :destroy
has_many :comments, :through => :posts
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
How I can fetch the user name that make a comments?
You are missing the Comment-belongs-to-User association:
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
This way you can fetch the commentor quite easily:
#comment.user
You can use delegate
class Comment < ActiveRecord::Base
belongs_to :post
delegate :user, to :post
end
Then in your code you can access
#comment.user

Model associations for a contest

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

Resources