How to find out user name - ruby-on-rails

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

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.

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

Ruby on Rails: Polymorphic Association Confusion

I am contributing one of the ruby on rails application over GitHub where I faced the following scenario:
I am having following models which I want to convert to make polymorphic:
class Comment < ActiveRecord::Base
belongs_to :team
belongs_to :user
belongs_to :application
belongs_to :project
end
class Team < ActiveRecord::Base
has_many :comments
end
class Project < ActiveRecord::Base
has_many :comments, -> { order('created_at DESC') }, dependent: :destroy
end
class User < ActiveRecord::Base
end
class Application < Rails::Application
end
I made following changes to make it polymorphic:
Perform database change to removed team_id, project_id, application_id and user_id and added commentable_id and commentable_type to comments table.
Modifications in models as described within rails guides.:
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
class Team < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Project < ActiveRecord::Base
has_many :comments, as: :commentable, -> { order('created_at DESC') }, dependent: :destroy
end
While I use it with default scope, It doesn't allow me to use with default scope and gives error with below line:
has_many :comments, as: :commentable, -> { order('created_at DESC') }, dependent: :destroy
I am confused to change in following models:
class User < ActiveRecord::Base
end
class Application < ActiveRecord::Base
end
Should I need following changes in User and Application model?
class User < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Application < ActiveRecord::Base
has_many :comments, as: :commentable
end
Thanks in Advance!
if your user/application object needs comments then add
class User < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Application < ActiveRecord::Base
has_many :comments, as: :commentable
end
else create belongs_to/has_many relationship not polymorphic
Eg.
class User < ActiveRecord::Base
has_many :comments
end
class Application < ActiveRecord::Base
has_many :comments
end

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

Simple Table Relation

Consider this:
class User < ActiveRecord::Base
# name, email, password
end
class Article < ActiveRecord::Base
# user_id, title, text
end
class Favorites < ActiveRecord::Base
# user_id, article_id
end
How do I put this all together in a way that I can have #user.articles (articles created by the user) and #user.favorite_articles (favorite articles from Favorite model)
Thanks in advance!
You can use a has_many :through association to fetch the favorite articles from user. You can also use it to fetch the users who favorited a given article.
class User < ActiveRecord::Base
has_many :articles
has_many :favorites
has_many :favorite_articles, :through => :favorites, :source => :article
end
class Article < ActiveRecord::Base
belongs_to :user
has_many :favorites
has_many :favorited_by_users, :through => :favorites, :source => :user
end
class Favorite < ActiveRecord::Base
belongs_to :article
belongs_to :user
end
If I understood right what you wanted, I'd say
class User < ActiveRecord::Base
has_many :articles
has_many :favorite_articles, :through => :favorites, :source => :article
end
class Article < ActiveRecord::Base
has_and_belongs_to_many :user
end
class Favorites < ActiveRecord::Base
has_and_belongs_to_many :user
has_one :article
end
edited: added favorite_articles

Resources