rails has_many and has_one - ruby-on-rails

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.

Related

The has_one :through Association! How Create connect between AccountHistory and Supplier

class Supplier < ApplicationRecord
has_one :account
has_one :account_history, through: :account
end
class Account < ApplicationRecord
belongs_to :supplier
has_one :account_history
end
class AccountHistory < ApplicationRecord
belongs_to :account
end
For some reason, in rails 6 can't create connect between Supplier and AccountHistory!
Need example how can create this connection if we have exist Supplier and exist AccountHistory
Thank you, everyone, I already found a solution!
Account.first.build_supplier(supplier_id: 1).save

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.

trying the rails model association

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

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

belongs_to relationship not working

I have following active record class
class Car < ActiveRecord::Base
belongs_to :owner
end
in the code when I try this
Car.first.owner
it gives me error "undefined method owner"
Can any one plz let me now if I'm missing any thing
You need to write the relation on the Owner side : has_one :car or has_many :cars depending on your needs.
class Car < ActiveRecord::Base
belongs_to :owner
end
class Owner < ActiveRecord::Base
has_one :car
end

Resources