How get data of model association rails - ruby-on-rails

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 :)

Related

how to show polymorphic record ruby on rails

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

ordered by child attribute, in polymorphic polymorphic association

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)

Rails 3 Find all records from has_many :through without collision from previous has_many

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

:has_many relationship with :through on another relationship with :through

My setup is as follows:
class User < ActiveRecord::Base
has_many :owners, :dependent => :destroy
has_many :properties, :through => :owners
end
class Owner < ActiveRecord::Base
belongs_to :user
belongs_to :property
end
class Property < ActiveRecord::Base
has_many :owners, :dependent => :destroy
has_many :users, :through => :owners
has_many :datafiles, :dependent => :destroy
end
class Datafile < ActiveRecord::Base
belongs_to :property
end
Now I'd like to be able to do #user.datafiles.
I tried has_many :datafiles, :through => :properties, :source => :datafiles but there appears to be a problem with a :through on something that's already went to a :through. So how would I go about to try and manage what I'm trying to do here?
Thank you in advance.
2 approaches;
1>
class User < AR
has_many :owners, :dependent => :destroy
has_many :properties, :through => :owners
has_many datafiles
end
class Datafile < AR
belongs_to :user
belongs_to :property
end
Your requirement of user.datafiles should be fulfilled with this.
If you want a nested has_many through, you'll need to use a plugin which is the 2nd approach.
2>
You can find it here.
The plugin works out of the box and does the job.
How about something like:
#user.rb
def datafiles
Property.find(:all, :joins => :owners, :conditions => ['owners.user_id = self.id'], :include => :datafile).collect(&:datafile)

How do I define ActiveRecord relationships between two models that relate to each other in two different ways?

In my app I have the classes User, Video, and Vote. Users and Videos can relate to each other in two different ways: as a one-to-many or as a many-to-many. The former is when a User submits a Video (one user can submit many videos). The latter is when a user votes on a video (users have many videos through votes, and vice versa). Here is my code, which does not work (I think -- I may be doing something wrong in the view). Please help me understand the correct way to structure these associations:
class User < ActiveRecord::Base
has_many :videos, :as => :submissions
has_many :votes #have tried it without this
has_many :videos, :as => :likes, :through => :votes
end
class Vote < ActiveRecord::Base
belongs_to :video
belongs_to :user
end
class Video < ActiveRecord::Base
belongs_to :user
has_many :votes #have tried it without this . . . superfluous?
has_many :users, :as => :voters, :through => :votes
end
I haven't gone and checked, but it goes something like this:
Instead of
has_many :videos, :as => :likes, :through => :votes
Use
has_many :likes, :class_name => "Video", :through => :votes
Same with the bottom:
has_many :users, :as => :voters, :through => :votes
becomes
has_many :voters, :class_name => "User", :through => :votes
:as is used for polymorphic associations. See this chapter in docs for more info.
class User < ActiveRecord::Base
has_many :videos # Submitted videos
has_many :votes
has_many :voted_videos, :through => :votes # User may vote down a vid, so it's not right to call 'likes'
end
class Vote < ActiveRecord::Base
belongs_to :video
belongs_to :user
end
class Video < ActiveRecord::Base
belongs_to :user
has_many :votes
has_many :voters, :through => :votes
end
More details can be found here: http://guides.rubyonrails.org/association_basics.html
Hope it helps =)
Thanks for your help guys, definitely pointed me in the right direction. Here is the working code:
class User < ActiveRecord::Base
has_many :videos, :as => :submissions
has_many :votes
has_many :likes, :source => :video, :through => :votes
end
class Vote < ActiveRecord::Base
belongs_to :video
belongs_to :user
end
class Video < ActiveRecord::Base
belongs_to :user
has_many :votes
has_many :voters, :source => :user, :through => :votes
end
PS I kept it as :likes because in this app they won't be able to downvote, only upvote.

Resources