I am attempting to do a query via a junction table, though Rails is given me the below error
Venue Model
class Venue < ActiveRecord::Base
attr_accessible :address, :latitude, :longitude, :name, :phone, :suburb, :state, :country
after_validation :geocode
has_many :orders, through: :venues_orders
geocoded_by :full_address
def full_address
[address, suburb, state, country].compact.join(', ')
end
end
Order Model
class Order < ActiveRecord::Base
attr_accessible :fulfilled, :item, :placed, :person_id, :special_instructions, :priority, :flag, :milk
belongs_to :person
belongs_to :venue
Venues Orders Model
class VenuesOrders < ActiveRecord::Base
attr_accessible :order_id, :venue_id
end
class Venue < ActiveRecord::Base
has_many :orders, through: :venues_orders
has_many :venues_orders
end
class Order < ActiveRecord::Base
has_many :venues, through: :venues_orders
has_many :venues_orders
end
class VenuesOrders < ActiveRecord::Base
belongs_to :venue
belongs_to :order
end
For more details read : RailsGuides
Another advice: The convention for creating a join table is lexical ordering. Like, OrdersVenues not VenuesOrders
Related
I have the following models:
class Department < ApplicationRecord
has_many :department_job_titles
has_many :job_titles, through: :department_job_titles
end
class JobTitle < ApplicationRecord
has_and_belongs_to_many :departments
end
class DepartmentJobTitle < ApplicationRecord
belongs_to :department
belongs_to :job_title
validates :department_id, uniqueness: { scope: :job_title_id }
end
This is erring w PG::UndefinedColumn: ERROR: column department_job_titles.title does not exist
LINE 1: ... "department_job_titles"."department_id" = $1 AND "departmen...
Department.first.department_job_titles.find_or_create_by(title: title)
DepartmentJobTitle has the following fields: id, department_id, job_title_id
What am I doing wrong here?
Try this:
job_title = JobTitle.find_or_create_by(title: title)
Department.first.job_titles << job_title unless job_title.in? Department.first.job_titles
Or that second line could be:
Department.first.job_titles = (Department.first.job_titles + [job_title]).uniq
Also:
class JobTitle < ApplicationRecord
has_many :department_job_titles
has_many :departments, through: :department_job_titles
end
... and ...
class DepartmentJobTitle < ApplicationRecord
belongs_to :department
belongs_to :job_title
validates :department, presence: true, uniqueness: { scope: :job_title }
validates :job_title, presence: true
end
... and think about what behaviour you want if someone destroys a JobTitle or Department -- either you want the DepartmentJobTitle destroyed also, or you want the destroy to be prevented, I expect.
class Subject < ActiveRecord::Base
attr_accessible :name, :teacher_id
has_and_belongs_to_many :courses
belongs_to :teacher
has_many :users, :through =>:feedback
has_many :feedbacks
end
class Course < ActiveRecord::Base
attr_accessible :name
has_many :users
has_and_belongs_to_many :subjects
validates :name, presence: true, length: { maximum: 50 }
end
class Feedback < ActiveRecord::Base
attr_accessible :rating, :recommendations, :strengths, :subject_id, :user_id, :weaknesses
belongs_to :user
belongs_to :subject
end
class User < ActiveRecord::Base
attr_accessible :course_id, :email, :gender, :name, :password, :password_confirmation
has_secure_password
belongs_to :course
has_many :feedbacks
has_many :subjects, :through =>:feedback
end
There is a join table between course and subjuct
I am a student and freshman in rails,I have some problem in this final project
How to
List subjects in order of number of feedbacks.
List students with no feedbacks.
I have no idea how to do it. Thanks very much
I am providing solution for two queries you need:
List subjects in order of number of feedbacks
#subjects = Subject.find(:all, :include=>:feedbacks).sort_by { |p| p.feedbacks.size}
List students with no feedbacks
User.find(:all, :include => :feedbacks, :conditions => "feedbacks.id is null")
I have a has many through relationship in my app:
Shows has many Bands through => Lineups
Bands are unique by :name
class Show < ActiveRecord::Base
attr_accessible :city_id, :title, :dateonly, :timeonly, :image, :canceled, :venue_attributes, :bands_attributes
belongs_to :city
belongs_to :venue
has_many :lineups
has_many :bands, through: :lineups
has_and_belongs_to_many :users
end
class Lineup < ActiveRecord::Base
belongs_to :show
belongs_to :band
end
class Band < ActiveRecord::Base
attr_accessible :name, :website, :country, :state
has_many :lineups
has_many :shows, through: :lineups
validates :name, presence: true
validates_uniqueness_of :name
before_save :titleize_name
private
def titleize_name
self.name = self.name.titleize
end
end
New Bands are created like this:
(lets say we have a show record already saved called s1)
> s1.bands.new(name: "Wet Food")
> s1.save
Right now this will only save if a band named "Wet Food" doesn't already exist
In which model is the best place to do a Band.find_or_create in this relationship so that an existing band can be used if one with the same name exists?
This is generally the type of call that would go in a Controller (or maybe a service object), but not in a Model. It really depends on the particular user flow that you're trying to accomplish in your app. Basically, where ever you are already using s1.bands.new, you could use this instead :
s1.bands.where(name: 'Wet Food').first_or_create
Here is my three models/tables
class Swimming::Classschedule < ActiveRecord::Base
belongs_to :swimming_classtimes ,:class_name=>'Swimming::Classtime',:foreign_key => "classtime_id"
attr_accessible :id,:coach_id, :level_id, :note, :classtime_id
end
class Swimming::Classtime < ActiveRecord::Base
has_many :swimming_classschedules,:class_name=>'Swimming::Classschedule'
belongs_to :swimming_timeblocks ,:class_name=>'Swimming::Timeblock',:foreign_key => "timeblock_id"
attr_accessible :date, :end, :start,:timeblock_id,:id
end
class Swimming::Timeblock < ActiveRecord::Base
has_many :swimming_classtimes,:class_name=>'Swimming::Classtime'
attr_accessible :name,:id
end
swimming_classschedules belongs_to swimming_classtimes
swimming_classtimes belongs_to swimming_timeblocks
swimming_timeblocks has_many swimming_classtimes
swimming_classtimes has_many swimming_classschedules
How are swimming_classschedules and swimming_timeblocks associated ?
swimming_timeblocks has_many swimming_classschedules through swimming_classtimes
class Swimming::Timeblock < ActiveRecord::Base
has_many :swimming_classtimes,:class_name=>'Swimming::Classtime'
has_many :swimming_classschedules,:through => :swimming_classtimes
attr_accessible :name,:id
end
I'm having trouble setting up this association between my models.
A User has many Accommodations, and Accommodations have one User.
Accommodations have many Notifications, and Notifications have one Accommodation.
Requests have many Notifications.
How can I make it so that I can get all of the Requests for a given User ( that is, User -> Accommodations (each) -> Notification -> Request)?
Update:
Here's my current controller file:
class PanelController < ApplicationController
before_filter :login_required
def index
#accommodations = current_user.accommodations.all
#requests = Array.new
#accommodations.each do |a|
a.notifications.each do |n|
#requests << Request.where('id' => n.request_id)
end
end
end
end
And models:
models/user.rb
class User < ActiveRecord::Base
[snip]
has_many :accommodations
has_many :notifications,
:through => :accommodations
end
models/accommodation.rb
class Accommodation < ActiveRecord::Base
validates_presence_of :title, :description, :thing, :location, :spaces, :price, :photo
attr_accessible :photo_attributes, :title, :description, :thing, :location, :spaces, :price
has_one :photo
has_many :notifications
belongs_to :user
accepts_nested_attributes_for :photo, :allow_destroy => true
end
models/notification.rb
class Notification < ActiveRecord::Base
attr_accessible :accommodation_id, :request_id
has_one :request
belongs_to :accommodation
end
models/request.rb
class Request < ActiveRecord::Base
belongs_to :notifications
attr_accessible :firstname, :lastname, :email, :phone, :datestart, :dateend, :adults, :children, :location, :status
validates_presence_of :firstname, :lastname, :email, :phone, :datestart, :dateend, :children, :adults, :location
end
Something like this should work:
#reqs = []
#user.accommodations.all.each do |a|
#reqs << a.notification.request
end
Assuming this is correct:
class User
has_many :accommodations
end
class Accommodation
belongs_to :user
has_many :notifications
end
class Notification
belongs_to :accomodation
belongs_to :request
end
class Request
has_many :notifications
end
Using has_many :through will not work for multiple models, as seen here: Ruby-on-Rails: Multiple has_many :through possible?
But you can do something like this in your user model:
class User
has_many :accommodations
has_many :notifications,
:through => :accommodations
def requests
self.notifications.all.collect{|n| n.request }
end
end