has-many-through

Rails has_many :through Find by Extra Attributes in Join Model

拟墨画扇 提交于 2019-11-26 17:07:04
New to both Ruby and Rails but I'm book educated by now (which apparently means nothing, haha). I've got two models, Event and User joined through a table EventUser class User < ActiveRecord::Base has_many :event_users has_many :events, :through => :event_users end class EventUser < ActiveRecord::Base belongs_to :event belongs_to :user #For clarity's sake, EventUser also has a boolean column "active", among others end class Event < ActiveRecord::Base has_many :event_users has_many :users, :through => :event_users end This project is a calendar, in which I have to keep track of people signing

how to add records to has_many :through association in rails

心已入冬 提交于 2019-11-26 11:48:41
问题 class Agents << ActiveRecord::Base belongs_to :customer belongs_to :house end class Customer << ActiveRecord::Base has_many :agents has_many :houses, through: :agents end class House << ActiveRecord::Base has_many :agents has_many :customers, through: :agents end How do I add to the Agents model for Customer ? Is this the best way? Customer.find(1).agents.create(customer_id: 1, house_id: 1) The above works fine from the console however, I don\'t know how to achieve this in the actual

Ruby-on-Rails: Multiple has_many :through possible?

守給你的承諾、 提交于 2019-11-26 10:34:33
问题 Is it possible to have multiple has_many :through relationships that pass through each other in Rails? I received the suggestion to do so as a solution for another question I posted, but have been unable to get it to work. Friends are a cyclic association through a join table. The goal is to create a has_many :through for friends_comments , so I can take a User and do something like user.friends_comments to get all comments made by his friends in a single query. class User has_many