I have a genre and movie models they are associated with has many through. when calling from browser i'm receiving error about categorization which is join model.
Error: undefined local variable or method `categorizations' for Class:0x00000007be0120>
This is the controller...
class GenreController < ApplicationController
def updateList
result=Net::HTTP.get(URI.parse('url'))
json = JSON.parse(result)
json['genres'].each do |data|
obj = Genre.new(
tmdb_id: data['id'],
name: data['name']
)
end
end
end
this is model
class Genre < ActiveRecord::Base
has_many :categorizations
has_many :movies, :through => categorizations
end
i know that there should be validations for this but right now i'm just populating genre table. There should be a create but i'm just trying to see the result.
EDIT:
Model for categorization
class Categorization < ActiveRecord::Base
belongs_to :movie
belongs_to :genre
end
Model for Movies
class Movie < ActiveRecord::Base
has_many :roles
has_many :actors, :through => :roles
has_many :watchlists
has_many :users, :through => :watchlists
has_many :categorizations
has_many :genres, :through => :categorizations
has_many :videos
end
has_many :movies, :through => categorizations
You need to have a colon before "categorizations":
has_many :movies, :through => :categorizations
Related
Doing an ActiveRecord join in RoR seems to work if I look at the generated SQL.
But what I can't figure out is why the result of that SQL isn't returned into the variable.
What I'm doing is:
class Book < ActiveRecord::Base
has_many :readings, dependent: :destroy
has_many :readers, :through => :readings
accepts_nested_attributes_for :readings
end
class Reader < ActiveRecord::Base
has_many :readings, dependent: :destroy
has_many :books, :through => :readings
accepts_nested_attributes_for :books
end
class Reading < ActiveRecord::Base
belongs_to :reader
belongs_to :book
end
Now, when asking:
result = Reading.where(:reader_id => rid, ).joins(:book).select(columns.collect{|c| c[:name]}.join(',')).flatten
It shows the correct generated SQL:
SELECT readings.id,books.title,books.author,readings.when FROM `readings` INNER JOIN `books` ON `books`.`id` = `readings`.`book_id` WHERE `readings`.`reader_id` = 2
BUT: the result variable only contains the values of the Reading record, NOT the fields of the joined table.
What am I missing?
I have made the association changes in question also:-
class Book < ActiveRecord::Base
has_many :readings, dependent: :destroy
has_many :readers, :through => :readings
accepts_nested_attributes_for :readings
end
class Reader < ActiveRecord::Base
has_many :readings, dependent: :destroy
has_many :books, :through => :readings
accepts_nested_attributes_for :books
end
class Reading < ActiveRecord::Base
belongs_to :reader
belongs_to :book
end
Query in this way:-
reader = Reader.find(rid)
result = reader.books.pluck(:name).join(',')
Ultimately, I've rewritten my helper class and fetched the various fields as I needed them. (as krishnar suggested)
Anyways: Thanx you guys for your contributions.
I have these models
class EventGroups < ActiveRecord::Base
has_many :festival_venues
has_many :venues, :through => :festival_venues
end
class Venue < ActiveRecord::Base
has_many :festival_venues
has_many :event_groups, :through => :festival_venues
end
class FestivalVenue < ActiveRecord::Base
belongs_to :event_group
belongs_to :venue
end
Now I want to create a Venue via Eventgroups, and record in the FestivalVenue should be created as well.
When i delete Eventgroups related record in Venue and
FestivalVenue should be deleted as well.
How can i do this?
class EventGroup < ActiveRecord::Base
has_many :festival_venues, dependent: :destroy
has_many :venues, :through => :festival_venues, :dependent => :destroy
end
class Venue < ActiveRecord::Base
has_many :festival_venues
has_many :event_groups, :through => :festival_venues
end
class FestivalVenue < ActiveRecord::Base
belongs_to :event_group
belongs_to :venue
end
Now if you have event_group variable bound to EventGroup object, you create Venue (along with its FestivalVenue) with:
venue = Venue.create(your_attributes)
event_group.venues << venue
In your below code, Model class name must be singular. Change class name EventGroups to EventGroup. Now it will work like a charm.
class EventGroup < ActiveRecord::Base
has_many :festival_venues, dependent: :destroy
has_many :venues, :through => :festival_venues, :dependent => :destroy
end
Remaining code is good.
class Venue < ActiveRecord::Base
has_many :festival_venues
has_many :event_groups, :through => :festival_venues
end
class FestivalVenue < ActiveRecord::Base
belongs_to :event_group
belongs_to :venue
end
Hope it will help. Thanks
How should I create following model in Rails 3.2? Project can have 1+ owners and 1+ users. Both of them are instances of class Person. I've thought about has_and_belongs_to_many but I don't know how to handle two separate collections of Persons for each Project.
You'll need a join model to represent each has-and-belongs-to-many relationship, and you would access using has-many-through as described here:
class ProjectOwnerLink < ActiveRecord::Base
belongs_to :project
belongs_to :owner, class_name: 'Person'
end
class ProjectUserLink < ActiveRecord::Base
belongs_to :project
belongs_to :user, class_name: 'Person'
end
class Project < ActiveRecord::Base
has_many :project_owner_links
has_many :owners, :through => :project_owner_links
has_many :project_user_links
has_many :users, :through => :project_user_links
end
class Person < ActiveRecord::Base
has_many :project_owner_links
has_many :owned_projects, :through => :project_owner_links, :source => :project
has_many :project_user_links
has_many :used_projects, :through => :project_user_links, :source => :project
end
You could define another model Participation that holds the type of the relationship, i.e. the role of the user. (Untested) code:
class Project < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
def with_role(role)
includes(:participations).where('participation.role = ?', role)
end
def owners
users.with_role('owner')
end
def participants
users.with_role('participant')
end
end
class User < ActiveRecord::Base
has_many :participations
has_many :projects, :through => :participations
def with_role(role)
includes(:participations).where('participation.role = ?', role)
end
def projects_owned
projects.with_role('owner')
end
def projects_participating_in
projects.with_role('participant')
end
end
class Participation < ActiveRecord::Base
# has an attribute 'role'
belongs_to :project
belongs_to :user
end
Below is the demo application.
https://github.com/diatmpravin/habtm-demo.git
Please have a look, Let me know if you have any question?
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
I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own.
Given the three model classes with their associations:
# application_form.rb
class ApplicationForm < ActiveRecord::Base
has_many :questions, :through => :form_questions
end
# question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :application_forms, :through => :form_questions
end
# form_question.rb
class FormQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :application_form
belongs_to :question_type
has_many :answers, :through => :form_question_answers
end
But when I execute the controller to add questions to application forms, I get the error:
ActiveRecord::HasManyThroughAssociationNotFoundError in Application_forms#show
Showing app/views/application_forms/show.html.erb where line #9 raised:
Could not find the association :form_questions in model ApplicationForm
Can anyone point out what I am doing wrong?
In the ApplicationForm class, you need to specify ApplicationForms's relationship to 'form_questions'. It doesn't know about it yet. Anywhere you use the :through, you need to tell it where to find that record first. Same problem with your other classes.
So
# application_form.rb
class ApplicationForm < ActiveRecord::Base
has_many :form_questions
has_many :questions, :through => :form_questions
end
# question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :form_questions
has_many :application_forms, :through => :form_questions
end
# form_question.rb
class FormQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :application_form
belongs_to :question_type
has_many :form_questions_answers
has_many :answers, :through => :form_question_answers
end
That is assuming that's how you have it set up.
You need to include a
has_many :form_question_answers
In your FormQuestion model. The :through expects a table that's already been declared in the model.
Same goes for your other models -- you can't supply a has_many :through association until you've first declared the has_many
# application_form.rb
class ApplicationForm < ActiveRecord::Base
has_many :form_questions
has_many :questions, :through => :form_questions
end
# question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :form_questions
has_many :application_forms, :through => :form_questions
end
# form_question.rb
class FormQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :application_form
belongs_to :question_type
has_many :form_question_answers
has_many :answers, :through => :form_question_answers
end
It looks like your schema might be a bit wonky, but the point is you always need to add the has_many for the join table first, then add the through.