has-many-through

how to add records to has_many :through association in rails

五迷三道 提交于 2019-11-27 06:00:15
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 application. Imagine a form is filled for the customer that also takes house_id as input. Then do I do the

Rails forms for has_many through association with additional attributes?

元气小坏坏 提交于 2019-11-27 05:30:30
问题 How can I generate form fields for a has_many :through association that has additional attributes? The has_many :through relationship has an additional column called weight . Here's the migration file for the join table: create_table :users_widgets do |t| t.integer :user_id t.integer :widget_id t.integer :weight t.timestamps end The models look like this: User has_many :widgets, :through => :users_widgets, :class_name => 'Widget', :source => :widget has_many :users_widgets accepts_nested

Rails has_many :through nested form

走远了吗. 提交于 2019-11-27 04:18:04
问题 I have just jumped into has_many :through association. I'm trying to implement the ability to save data for all 3 tables ( Physician , Patient and association table) through a single form. My migrations: class CreatePhysicians < ActiveRecord::Migration def self.up create_table :physicians do |t| t.string :name t.timestamps end end end class CreatePatients < ActiveRecord::Migration def self.up create_table :patients do |t| t.string :name t.timestamps end end end class CreateAppointments <

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

邮差的信 提交于 2019-11-27 03:31:39
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 :friendships has_many :friends, :through => :friendships, :conditions => "status = #{Friendship::FULL}" has_many

using has_many :through and build

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 01:41:21
问题 i have three models, all for a has_many :through relationship. They look like this: class Company < ActiveRecord::Base has_many :company_users, dependent: :destroy has_many :users, through: :company_users accepts_nested_attributes_for :company_users, :users end class CompanyUser < ActiveRecord::Base self.table_name = :companies_users #this is because this was originally a habtm relationship belongs_to :company belongs_to :user end class User < ActiveRecord::Base # this is a devise model, if

accepts_nested_attributes_for with has_many => :through Options

我的梦境 提交于 2019-11-27 01:05:47
问题 I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model. Associations: class Link < ActiveRecord::Base has_many :tags, :through => :link_tags has_many :link_tags accepts_nested_attributes_for :tags, :allow_destroy => :false, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } end class Tag < ActiveRecord::Base has_many :links, :through => :link_tags has_many :link_tags end class LinkTag < ActiveRecord::Base belongs_to :link

Need data from rails join table, has_many :through

旧巷老猫 提交于 2019-11-26 22:54:52
问题 I have 3 tables - users, things, and follows. Users can follow things through the follows table, associating a user_id with a things_id . This would mean: class User has_many :things, :through => :follows end class Thing has_many :users, :through => :follows end class Follow belongs_to :users belongs_to :things end So I can retrieve thing.users with no problem. My issue is if in the follows table, I have a column named "relation", so I can set a follower as an "admin", I want to have access

How to Implement a Friendship Model in Rails 3 for a Social Networking Application?

情到浓时终转凉″ 提交于 2019-11-26 22:32:54
问题 I'm currently working on a small social networking application and right now I'm trying to create a model that represents friendships between users . This is what I came up with so far: class User < ActiveRecord::Base # ... has_many :friendships has_many :friends, :through => :friendships end class Friendship < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => 'User' end My friendship model has a field confirmed as boolean which I'd like to use to define a friendship as

Many to Many Relationships with Ember, ember-data and Rails

。_饼干妹妹 提交于 2019-11-26 21:04:56
问题 I am having an issue with trying to save many to many relationships in Ember.js using ember-data and rails. The association works fine on the ember side of things, however when I try to commit the transaction it will not include the list of new associations when submitting to rails. Any help would be much appreciated, I've been tearing out my hair trying to find an example application that does something this simple on github but I can't seem to find one. Here is a dumbed down version of my

Rails - Sort by join table data

≯℡__Kan透↙ 提交于 2019-11-26 20:14:06
问题 I've got a RoR project in the works. Here are the applicable sections of my models. Home has_many :communities, :through => :availabilities has_many :availabilities, :order => "price ASC" Community has_many :homes, :through => :availabilities has_many :availabilities Availability belongs_to :home belongs_to :community The "availabilities" table in the database has the additional data column "price" So now I can call @home.availabilities.each do |a| a.community.name a.price and get back the