I am working on a rails app that registers campers for a camp. First you sign up using your name, email, etc. Once your profile is made, you can register for a camp. If the camp is not already full, then you are immediately enrolled. If it is full, then you are put on a waitlist. Right now I have 4 models: Campers (name, email, etc.), Camps (name, location, etc.), Enrollments, and Waitlists.
The idea is to have a camper be able to register to many camps, and obviously a camp having many campers enrolled or waitlisted in it. Here are my classes:
# camper.rb
has_many :enrolled_in, :class_name => 'Camps', through: :enrollments, dependent: :destroy
has_many :waitlisted_in, :class_name => 'Camps', through: :waitlists, dependent: :destroy
# camp.rb
has_many :enrolled_campers, :class_name => 'Camper', through: :enrollments
has_many :waitlisted_campers, :class_name => 'Camper', through: :waitlists
I'm having trouble with accessing these models through the views. Here is what show.html.erb looks like:
<!-- Listing camps -->
<h2>Camps</h2>
<p>
<strong>Name:</strong>
<%= #camper.enrolled_in.name %> <!-- This is where I get the error -->
</p>
<!-- Adding camps -->
<h2>Add a camp:</h2>
<%= form_with(model: [#camper, #camper.enrolled_in.build ]) do |form| %>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
But I'm getting the following error:
ActiveRecord::HasManyThroughSourceAssociationNotFoundError in Campers#show
Could not find the source association(s) "enrolled_in" or :enrolled_in in model Enrollment. Try 'has_many :enrolled_in, :through => :enrollments, :source => '. Is it one of camp or camper?
And I honestly can't tell what's going wrong. I'm fairly new to databases and rails, so go easy on me.
Look to direction of HABTM associations.
This will make your code cleaner.
You will have :after_add and :after_destroy actions that will always track model changes (even direct foreign key inserting, unlike in same named has_many callbacks.)
So.
class Camper
has_and_belongs_to_many :waitlists, after_add: :check_camp_ready_to_start_as_example
has_and_belongs_to_many :enrolllists
end
class Waitlist
belongs_to :camp
has_and_belongs_to_many :campers
end
class Enrolllist
belongs_to :camp
has_and_belongs_to_many :campers
end
class Campl
has_many :enrolllists
has_many :waitlists
end
How to create such migrations (need to create join tables), you can read here
Related
I keep getting this annoying error consistently and I cannot solve it. I recently posted a question on the same topic and got no productive help.
I want users to request to join a group. Cliqs = Groups. All of my console tests seem correct, but I cannot seem to find a solution to my problem. The association is showing up, but I can't seem to get the update/accept method to run.
This is driving me crazy! How do I fix this?
Here is my code:
My Models:
class User < ActiveRecord::Base
has_many :uploads
has_one :owned_cliq, foreign_key: 'owner_id', class_name: 'Cliq', dependent: :destroy
has_many :cliq_memberships, dependent: :destroy
has_many :cliqs, through: :cliq_memberships
has_many :cliq_requests, dependent: :destroy
...
end
class Cliq < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
has_many :cliq_memberships, dependent: :destroy
has_many :members, through: :cliq_memberships, source: :user
has_many :cliq_requests, dependent: :destroy #cliq_request_sender
has_many :pending_members, through: :cliq_requests, source: :user, foreign_key: 'user_id'
end
class CliqRequest < ActiveRecord::Base
#from
belongs_to :user
#to
belongs_to :cliq
#validate :not_member
#validate :not_pending
def accept
cliq.members << pending_member
destroy
end
end
My controller:
class CliqRequestsController < ApplicationController
def index
#incoming
##cliq_requests_received = CliqRequest.where(cliq: cliq)
#outgoing
##cliq_requests_sent = current_user.cliq_requests
end
def show
end
def create
cliq = Cliq.find_by(params[:id])
#cliq_request = current_user.cliq_requests.new(cliq: cliq)
if #cliq_request.save
redirect_to current_user #change to cliqs/cliq path later
else
redirect_to cliq_path
end
end
def update
#cliq = Cliq.find_by(id: params[:cliq_id])
#cliq_request = #cliq.cliq_requests.find_by(id: params[:id])
#cliq_request.accept
end
def destroy
#cliq_request.destroy
end
end
My View:
<h1><%= #cliq.name %></h1>
<%= link_to 'Request to join Cliq', '/cliqs/:cliq_id/cliq_requests', :method => :post %>
<% #cliq_members.each do |cliq_member| %>
<ul><%= link_to cliq_member.username, user_path(cliq_member) %></ul>
<% end %>
<% if #current_user = #cliq.owner %>
<% #cliq.pending_members.each do |pending_member| %>
<ul><%= link_to pending_member.username, user_path %>
<%= link_to "Accept", "/cliqs/:cliq_id/cliq_requests/:id/", :method => :put %>
<%= link_to "Deny", "/cliqs/:cliq_id/cliq_requests/:id/", :method => :delete %>
</ul>
<% end %>
<% end %>
My Routes:
resources :cliqs do
resources :cliq_requests
end
These lines appear malformed:
<%= link_to 'Request to join Cliq', '/cliqs/:cliq_id/cliq_requests', :method => :post %>
<%= link_to "Accept", "/cliqs/:cliq_id/cliq_requests/:id/", :method => :put %>
<%= link_to "Deny", "/cliqs/:cliq_id/cliq_requests/:id/", :method => :delete %>
I recommend you use path helpers [e.g. cliq_cliq_request_path(cliq, cliq_request) if you are using resourceful routing]. You can use rake routes for help. If you are seeing things like :cliq_id and and :id in your development.log or test.log as part of the URLs that are hit, those should instead be numbers. You can also interpolate the strings yourself (e.g. "/cliqs/#{cliq_id}/cliq_requests/#{cliq_request.id}") but this is usually more typing and certainly more fragile over time.
One of your problems may be that you are looping through a list of pending member names, which doesn't have all the data you need to form the link correctly. So your update action may be working fine, but you may not be passing it the right data.
Also this line:
if #current_user = #cliq.owner
is an assignment, and so will always return true. Presumably you mean ==
I'm a new guy to ruby on rails and working on my first in-depth application. It has four tables: Questions, Options, Answers and Users. There's a list of questions and a user can vote for a unique option (stored in the Answers join table), I'm trying to get my head around table associations.
This is how I've setup my individual RB files:
class Question < ActiveRecord::Base
has_many :options
has_many :answers, :through => :options
end
class Option < ActiveRecord::Base
belongs_to :question
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
belongs_to :option
end
class User < ActiveRecord::Base
has_many :answers
has_many :questions, :through => :answers
end
My questions controller is setup like this to include the options table:
#questions = Question.includes(:options).all
and the table body in my index.html.erb file:
<tbody>
<% #questions.each do |question| %>
<tr class="<%= cycle('lineOdd', 'lineEven') %>">
<td><%= question.question_text %></td>
<td><%= link_to 'Show', question %></td>
<td><%= link_to 'Edit', edit_question_path(question) %></td>
<td><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% question.options.each do |option_text| %>
<tr class="backgroundColor1">
<td class="optionCell"> <%= option_text.option_text %> </td>
</tr>
<% end %>
<% end %>
</tbody>
In the Question class I've used 'has_many :answers, :through => :options' - is this the correct way to go about this and how would I output the total number of votes in a table row below the associated option.
Do I need to add to or change the question controller code?
This is my first post, sorry if I'm not informative enough!
Thanks
Lets start by fixing up the relations a bit:
class Question < ActiveRecord::Base
has_many :options
has_many :answers
has_many :users, through: :answers
end
There is nothing technically wrong with has_many :answers, :through => :options but since there is a direct relation through answers.question_id we don't need to go through the options table for the relation.
Displaying the count
If we simply did:
<td class="optionCell"><%= option.answers.count %></td>
This would create a nasty n+1 query to fetch the count of the answers for each option. So what we want to do is create a counter cache which stores a tally on the options table.
Lets start by creating a migration to add the column:
rails g migration AddAnswerCounterCacheToOptions answers_count:integer
rake db:migrate
Then we tell ActiveRecord to update the tally when we create associated records, this looks a bit strange since the counter_cache: true declaration is on the belongs_to side while the column is on the other but thats just how AR works.
class Option < ActiveRecord::Base
belongs_to :question
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
belongs_to :option, counter_cache: true
end
There is a little snag here. Since we may already have records we need to make sure they have correct counters. You can do this from the console but in the long run it is a good idea to create a rake task.
Option.find_each { |option| Option.reset_counters(option.id, :answers) }
This might take a bit of time since it needs to pull each Option and update the count.
Now we can display the tally like so:
<% question.options.each do |option| %>
<tr class="backgroundColor1">
<td class="optionCell"><%= option.option_text %></td>
<td class="optionCell"><%= option.answers.size %></td>
</tr>
<% end %>
.size is smart enough to use our counter cache column, but will fall back to querying the count which is a good thing for tests.
I have a User table in Rails and it has 2 user types. I associated them with polymorphic associations, my models are:
class User < ActiveRecord::Base
belongs_to :owner, polymorphic: true
class Buyer < ActiveRecord::Base
has_one :user, as: :owner, dependent: :destroy
accepts_nested_attributes_for :user
class Seller < ActiveRecord::Base
has_one :user, as: :owner, dependent: :destroy
accepts_nested_attributes_for :user
I need a registration form in HTML for new users and automatically specify their type (each user type has their own registration link)
How do I manage the controllers and HTML form to do this? The user is going to fill the form with information for the User and Buyer or Seller model.
Thank you
You need 2 routes, one for buyer and another for seller. And then you can use Rail's form helpers:
<%= form_for :buyer do |f| %>
<%= f.fields_for :user %>
[your user fields here]
<% end %>
<% end %>
The form is analogous for the seller.
I was hoping someone could help me with this, been trying to figure it out for a week now, I found a lot of examples, but as I'm new to rails I guess I keep making a mistake somewhere and I just cant find a right solution for my case.
So I have:
class Blog < ActiveRecord::Base
attr_accessible :name, :subject_id, :created_at
has_many :blogs_messages
has_many :messages, through: :blogs_messages
end
class Message < ActiveRecord::Base
attr_accessible :title, :body, :created_at
has_many :blogs_messages
has_many :blogs, through: :blogs_messages
end
class BlogsMessages < ActiveRecord::Base
attr_accessible :message_id, :blog_id
belongs_to :blog
belongs_to :message
end
Messages live in different Blogs(like Pink Blog, Green Blog, Maroon Blog etc), and Blogs live in Subjects (Dark Colors, Bright Colors etc)
Subjects have many Blogs, but Blogs can belong only to one Subject.
BlogsMessages is the connection between Messages and Blogs
what im trying to do is to show:
top 3 Blogs (by amount of messages in them) within one Subject
so e.g. when I want to choose Subject Dark Colors it will show me:
1.Maroon Blog: 46 messages
2.Grey Blog: 13 messages
3.Purple Blog: 12 messages
(There are 8 Blogs altogether in Subject Dark Colors.)
Could someone please help me with this, or at least point me in the right direction how to make it all work?
Update:
in my Blogs_controller now i have:
#blogs = Blog.joins(:blogs_messages => :message).select('blogs.*, COUNT(messages.id) AS message_count').group('blog_id').order('COUNT(messages.id) DESC').limit(3)
in my blogs view:
<% #blogs.each do |blog| %>
<li><%= blog.name %>: messages</li>
<% end %>
I'm not sure this can work because I can't test it but it may help you:
Blog.where(subject_id: subject.id)
.joins(:blogs_messages => :message)
.select('blogs.*, COUNT(messages.id) AS message_count')
.group(:blog_id)
.order('message_count DESC')
.limit(3)
Also, in the view you could access to the new virtual attribute message_count:
<% #blogs.each do |blog| %>
<li><%= blog.name %>: <%= blog.message_count %> messages</li>
<% end %>
quick question that should be simple to answer despite trouble i've had:
i have a simple rails app with a message ('intro') tab displaying sent and received messages ('intros'). i have the messages routing from user to user appropriately, and the content of the messages is displaying fine in user inboxes. however, i'm having trouble showing the name's of the users associated with the messages next to the messages themselves
i have a User model:
class User < ActiveRecord::Base
attr_accessible :name, :email, :one_liner, :password, :password_confirmation
has_secure_password
has_many :sent_intros, foreign_key: "sender_id", dependent: :destroy, class_name: "Intro"
has_many :received_intros, foreign_key: "receiver_id", dependent: :destroy, class_name: "Intro"
has_many :receivers, through: :sent_intros, source: :receiver
has_many :senders, through: :received_intros, source: :sender
...
, an Intro (message) model:
class Intro < ActiveRecord::Base
attr_accessible :content, :receiver_id, :sender_id
belongs_to :sender, class_name: "User"
belongs_to :receiver, class_name: "User"
...
and here is the relevant code from the users controller:
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy
def show
#user = User.find(params[:id])
#intro = Intro.find(params[:id])
#sent_intros = current_user.sent_intros.paginate(page: params[:page])
#received_intros = current_user.received_intros.paginate(page: params[:page])
end
...
my .erb show page:
<% provide(:title, #user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= #user.name %>
</h1>
</section>
</aside>
<div class="span8">
<% if#user.received_intros.any? %>
<h3>Received intros (<%= #user.received_intros.count %>)</h3>
<ol class="intros">
<%= render #received_intros %>
</ol>
<%= will_paginate #received_intros %>
<% end %>
<% if#user.sent_intros.any? %>
<h3>Sent intros (<%= #user.sent_intros.count %>)</h3>
<ol class="intros">
<%= render #sent_intros %>
</ol>
<%= will_paginate #sent_intros %>
<% end %>
</div>
</div>
so I'm concerned with the <%= render #received_intros %> and <%= render #sent_intros %> lines of this page
currently, it displays the following (intro content without the associated user):
how do I get prefix those usernames to their respective intros? thanks!
Looks like you're looking up Intro based on the same id as User in your controller actions. Since it's looked up second, it's overwriting the #user variable. Here's your code:
#user = User.find(params[:id])
#intro = Intro.find(params[:id])
I'm guessing you probably want that second line to be something like params[:intro_id], but not entirely sure without seeing the view code linking to that page and possibly your routes file.