I have 3 modules Blog, News, Article and I gathering reviews for all.
class Review < ActiveRecord::Base
attr_accessible :reviewable_type,:reviewable_id,:description,:context,:language,:status, :title, :user_id,:review_category_id,:ratings
belongs_to :user
belongs_to :review_category
belongs_to :reviewable, :polymorphic => true
class Blog < ActiveRecord::Base
attr_accessible :description, :status, :title
has_many :reviews, :as => :reviewable, :dependent => :destroy
class News < ActiveRecord::Base
attr_accessible :description, :status, :title
has_many :reviews, :as => :reviewable, :dependent => :destroy
class Article < ActiveRecord::Base
attr_accessible:description, :status, :title
has_many :reviews, :as => :reviewable, :dependent => :destroy
My question is, how to get all the reviews posted on current user's blog, article, news etc?
Just like how you associate other has_many relations. Like this:
current_user.blogs.first.reviews
current_user.articles.first.reviews
current_user.news.first.reviews
In your blog show page,
#blog.reviews.each |r|
r.review_text
end
Because you're trying to call for a user, I'd recommend looking at the User model too:
#app/models/user.rb
Class User < ActiveRecord::Base
#User can submit many reviews
has_many :reviews
#User can submit many blogs / news / articles
has_many :blogs
has_many :news
has_many :articles
def first_blog
blogs.first
end
end
#app/models/review.rb
class Review < ActiveRecord::Base
belongs_to :user
belongs_to :review_category
belongs_to :reviewable, :polymorphic => true
end
#app/models/blog.rb
class Blog < ActiveRecord::Base
belongs_to :user
has_many :reviews, :as => :reviewable, :dependent => :destroy
def first_review
reviews.first
end
end
#app/models/news.rb
class News < ActiveRecord::Base
belongs_to :user
has_many :reviews, :as => :reviewable, :dependent => :destroy
end
#app/models/article.rb
class Article < ActiveRecord::Base
belongs_to :user
has_many :reviews, :as => :reviewable, :dependent => :destroy
end
Reviews
This will allow you to call:
#########
#Reviews#
#########
#Submitted
current_user.reviews
#Received
current_user.first_blog.first_review
current_user.first_blog.reviews
Related
How can i get data of model association
this are my models.
user.rb
has_many :movie
has_many :quality
has_many :option
movie.rb
belongs_to :user
has_many :quality, :dependent => :destroy
quality.rb
belongs_to :movie
belongs_to :user
has_many :option
option.rb
belongs_to :user
belongs_to :quality
has_one :movie, :through => :quality
this is my controller
movie_controller.rb
def show
#movie = Movie.find(params[:id])
end
In my view i have this.
-#movie.qualities.option do |o|
=o.name
diagram
I want to get all the names of the qualities of a movie.
Please help me.
You need to change all has many relationship to plural
has_many :movies
has_many :qualities
has_many :options
# etc
and option belongs_to movie
Your final code should look like this
user.rb
has_many :movies
has_many :qualities, through: :movies
has_many :options, through: :qualities
movie.rb
belongs_to :user
has_many :qualities, dependent: :destroy
quality.rb
belongs_to :movie
has_one :user, through: :movie
has_many :options
option.rb
belongs_to :quality
has_one :user, through: :quality
has_one :movie, through: :quality
movie_controller.rb
def show
#movie = Movie.find(params[:id])
end
View should be like this.
- #movie.qualities.each do |quality|
- quality.options.each do |option|
= option.name
Cheers :)
i have the following models
class Airplane < ActiveRecord::Base
has_many :airtags
has_many :pictures, :through => :airtags
end
class Airtag < ActiveRecord::Base
attr_accessible :airable_type, :airable_id, :airplane_id
belongs_to :airplane
belongs_to :airable, :polymorphic => true
end
class Picture < ActiveRecord::Base
belongs_to :picturable, :polymorphic => true
has_many :airtags, :as => :airable, :dependent => :destroy
has_many :airplanes, :through => :airtags
end
in my airplane show, i want to list all pictures, ordered by their name.
#airplane.pictures.order(:name)
I have models in my app:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Project < ActiveRecord::Base
has_many :discussions, :dependent => :destroy
has_many :tickets, :dependent => :destroy
end
class Discussion < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
class Ticket < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
Everything works fine, but sometimes it's not very convinient to get project from comment through commentable, i.e. comment.commentable.project.
Is there any way to make has_one project in Comment model?
I would add the following method to your class Comment:
def project
self.commentable ? self.commentable.project : nil
end
This will give you the same result without all the magic of ActivRecord.
I have the following models:
class User < ActiveRecord::Base
has_many :books, :dependent => :destroy
has_many :favorites
has_many :books, :through => :favorites
end
class Favorite < ActiveRecord::Base
belongs_to :book
belongs_to :user
validates :user_id, :book_id, :presence => true
end
class Book < ActiveRecord::Base
belongs_to :user
belongs_to :favorite
end
The idea is that a user can own a book and add a book from another user as favorite. In rails console, i tried User.find(1).favorites.books but got a NoMethodError: undefined method books'. Anduser.books` only returns the books owned by that user
Is there any way to retrieve all books that belong to a user's favorite in this case?
You are very close, but you shouldn't have two associations name books. Try something like this:
class User < ActiveRecord::Base
has_many :books, :dependent => :destroy
has_many :favorites
has_many :favorite_books, :through => :favorites, :source => :book
end
Then your query would simply be User.find(1).favorites_books
Models:
class Thread < ActiveRecord::Base
has_many :threaded, :through => :threaded, :foreign_key => :thread_id
class ThreadFeed < ActiveRecord::Base
belongs_to :threaded, :polymorphic => true
Model Fields
Thread (id)
ThreadFeed (id, thread_id, threaded_id, threaded_type)
Problem is with:
#thread.threaded
Rails is using (threaded_id, threaded_type) as the foreign key and I want thread_id to be the foreign key.
Take a look into this Railscast, Polymorphism
It will give you a better insight into how Polymorphism works.
First issue I notice is that it should be through :threadfeed, not :threaded
class Thread < ActiveRecord::Base
has_many :threadfeeds, :as => :threaded
In the Railscast, he has:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Photo < ActiveRecord::Base
has_many :comments, :as => :commentable
#...
end
class Event < ActiveRecord::Base
has_many :comments, :as => :commentable
end
The first problem is that Thread doesn't know what feed is:
class Thread < ActiveRecord::Base
has_many :feeds, :through => :thread_feeds, :as => :feeded
has_many :thread_feeds
class ThreadFeed < ActiveRecord::Base
belongs_to :feeded, :polymorphic => true
The second problem is the complexity of polymorphic. Here's a great article on it: http://blog.hasmanythrough.com/2006/4/3/polymorphic-through