Could not find the association problem in Rails - ruby-on-rails

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.

Related

how to add records to has_many :through association in Ruby on rails

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 do you model "Likes" in rails?

I have 3 models: User, Object, Likes
Currently, I have the model: a user has many Objects. How do I go about modeling:
1) A user can like many objects
2) an Object can have many likes (from different users)
So I want to be able to do something like this:
User.likes = list of objects liked by a user
Objects.liked_by = list of Users liked by object
The model below is definitely wrong...
class User < ActiveRecord::Base
has_many :objects
has_many :objects, :through => :likes
end
class Likes < ActiveRecord::Base
belongs_to :user
belongs_to :object
end
class Objects < ActiveRecord::Base
belongs_to :users
has_many :users, :through => :likes
end
To elaborate further on my comment to Brandon Tilley's answer, I would suggest the following:
class User < ActiveRecord::Base
# your original association
has_many :things
# the like associations
has_many :likes
has_many :liked_things, :through => :likes, :source => :thing
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :thing
end
class Thing < ActiveRecord::Base
# your original association
belongs_to :user
# the like associations
has_many :likes
has_many :liking_users, :through => :likes, :source => :user
end
You are close; to use a :through, relation, you first must set up the relationship you're going through:
class User < ActiveRecord::Base
has_many :likes
has_many :objects, :through => :likes
end
class Likes < ActiveRecord::Base
belongs_to :user
belongs_to :object
end
class Objects < ActiveRecord::Base
has_many :likes
has_many :users, :through => :likes
end
Note that Objects should has_many :likes, so that the foreign key is in the right place. (Also, you should probably use the singular form Like and Object for your models.)
Here is a simple method to achieve this. Basically, you can create as many relationships as needed as long as you specify the proper class name using the :class_name option. However, it is not always a good idea, so make sure only one is used during any given request, to avoid additional queries.
class User < ActiveRecord::Base
has_many :likes, :include => :obj
has_many :objs
has_many :liked, :through => :likes, :class_name => 'Obj'
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :obj
end
class Obj < ActiveRecord::Base
belongs_to :user
has_many :likes, :include => :user
has_many :users, :through => :likes
# having both belongs to and has many for users may be confusing
# so it's better to use a different name
has_many :liked_by, :through => :likes, :class_name => 'User'
end
u = User.find(1)
u.objs # all objects created by u
u.liked # all objects liked by u
u.likes # all likes
u.likes.collect(&:obj) # all objects liked by u
o = Obj.find(1)
o.user # creator
o.users # users who liked o
o.liked_by # users who liked o. same as o.users
o.likes # all likes for o
o.likes.collect(&:user)
Models & associations as per naming conventions of rails modeling
class User < ActiveRecord::Base
has_many :likes
has_many :objects, :through => :likes
end
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :object
end
class Object < ActiveRecord::Base
belongs_to :user
has_many :likes
has_many :users, :through => :likes
end
Also, you can use of already built-in gems like acts-as-taggable-on to have same functionality without code :)

Uninitialized constant error on has_many through association

I've got a question about active-record association in Rails.
I'm developing three active-record model: Team, Teamuser, users, testing on "has_many through" association.
basically, I just want to be able to call team.users and user.teams.
here are my model definitions
team.rb
#######
class Team < ActiveRecord::Base
has_many :teamusers, :foreign_key => :team_id
has_many :users, :through => :teamusers
end
and
teamuser.rb
###########
class Teamuser < ActiveRecord::Base
belongs_to :teams
belongs_to :users
end
then
user.rb
########
class User < ActiveRecord::Base
has_many :teamusers, :foreign_key => :user_id
has_many :teams, :through => :teamusers
end
every time I try this
team.users
it returns me an error saying "uninitialized constant Team::Users".
what might I be wrong he?
any advice would be very much appreciated.
Your Teamuser should be:
class Teamuser < ActiveRecord::Base
belongs_to :team
belongs_to :user
end

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

Combining Polymorphic Associations with Rich Many-to-Many Associations

Right now I have a rich many-to-many association with VideoVote as the independent record.
class VideoVote < ActiveRecord::Base
belongs_to :user
belongs_to :video
end
class User < ActiveRecord::Base
has_many :video_votes
has_many :voted_videos,
:through => :video_votes,
:source => :video
end
class Video < ActiveRecord::Base
has_many :video_votes
has_many :voted_users,
:through => :video_votes,
:source => :user
end
However, I want to trasform this into a polymorphic association where comments can also have many VideoVotes (I realize this is confusing, so I should probably change it to Votes). (also, a video will have many comments.) How should I do this?
You first want to add voteable_id:integer and voteable_type:string to your video_votes table.
Then your models will look like:
class VideoVote < ActiveRecord::Base
belongs_to :voteable, :polymorphic => true
end
class Comment < ActiveRecord::Base
has_many :video_votes, :as => :voteable
#code
end
class Video < ActiveRecord::Base
has_many :video_votes, :as => :voteable
#code
end
Then you can access them just like any other has_many:
#video.video_votes
#comment.video_votes
#etc.

Resources