has-many-through

Active Record has_many :through remove one associated record

帅比萌擦擦* 提交于 2019-11-28 21:28:22
问题 This may be a very basic oversight on my part, but I can't seem to recall a simple method for removing an association between two objects joined via has_many :through . IE: class Photo has_many :tags, :through => :taggings has_many :taggings, :dependent => :destroy end class Tags has_many :photos, :through => :taggings has_many :taggings, :dependent => :destroy end class Taggings belongs_to :photo belongs_to :tag end If you have two objects, tag and photo , you can associate them just by

Rails 4 Form: has_many through: checkboxes

梦想与她 提交于 2019-11-28 18:12:28
Original question Two resources: users and animals. When creating a user, the client selects check boxes to say how many animals they have. When the user form is submitted, it should not only create a new user record, but it should also create a bunch of records in the animal_users rich join table to represent each of the check boxes the client selected. The issue I think is that I am not specifying something correctly for the checkbox part within the form. I have looked at the checkbox_tag API , the Rails Guides on Forms , and many websites and stackOverflow posts. Thanks in advance, code

Rails 3, nested multi-level forms and has_many through

北战南征 提交于 2019-11-28 17:07:24
I'm trying to get it to work but it dosen't! I have class User < ActiveRecord::Base has_many :events, :through => :event_users has_many :event_users accepts_nested_attributes_for :event_users end class Event < ActiveRecord::Base has_many :event_users has_many :users, :through => :event_users accepts_nested_attributes_for :users end class EventUser < ActiveRecord::Base set_table_name :events_users belongs_to :event belongs_to :user accepts_nested_attributes_for :events accepts_nested_attributes_for :users end And also the table-layout event_users user_id event_id user_type events id name users

Rails 3.2 has_many through form submission

筅森魡賤 提交于 2019-11-28 07:11:25
问题 I have a has_many :through form where I can't get an extra attribute to post to the database. I'm fouling up the parameter name somewhere. I can get the foreign keys to post but I have another attribute that I'm trying to track in the join table. Keep in mind this is a 100% ajax based form. Here's what I know Edit: After researching similar issues, I understand I'm supposed to build the form attributes, but the code I've found doesn't work for some reason. Here are some resources. http:/

accepts_nested_attributes_for with has_many => :through Options

六眼飞鱼酱① 提交于 2019-11-28 05:50:37
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 belongs_to :tag end links_controller Actions: def new @link = @current_user.links.build respond_to do

Rails forms for has_many through association with additional attributes?

陌路散爱 提交于 2019-11-28 05:08:46
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_attributes_for :widgets # not sure if this is necessary Widget has_many :users, :through => :users_widgets,

dependent => destroy on a “has_many through” association

喜你入骨 提交于 2019-11-28 04:38:36
Apparently dependent => destroy is ignored when also using the :through option. So I have this... class Comment < ActiveRecord::Base has_many :comment_users, :dependent => :destroy has_many :users, :through => :comment_users ... end ...but deleting a Comment does not result in the associated comment_user records getting deleted. What's the recommended approach, then, for cascade deletes when using :through? Thanks Apparently :dependent is not ignored! The real issue was that I was calling Comment.delete(id) which goes straight to the db, whereas I now use Comment.destroy(id) which loads the

How to set up factory in FactoryGirl with has_many association

主宰稳场 提交于 2019-11-28 03:22:13
Can someone tell me if I'm just going about the setup the wrong way? I have the following models that have has_many.through associations: class Listing < ActiveRecord::Base attr_accessible ... has_many :listing_features has_many :features, :through => :listing_features validates_presence_of ... ... end class Feature < ActiveRecord::Base attr_accessible ... validates_presence_of ... validates_uniqueness_of ... has_many :listing_features has_many :listings, :through => :listing_features end class ListingFeature < ActiveRecord::Base attr_accessible :feature_id, :listing_id belongs_to :feature

Laravel Polymorphic Relations Has Many Through

寵の児 提交于 2019-11-28 00:03:02
问题 I have a Subscriber Model // Subscriber Model id user_id subscribable_id subscribable_type public function user() { return $this->belongsTo('App\User'); } public function subscribable() { return $this->morphTo(); } And a Topic Model // Topic Model public function subscribers() { return $this->morphMany('App\Subscriber', 'subscribable'); } And I want get all users through subscriber model, to notify them like Notification::send($topic->users, new Notification($topic)); // Topic Model public

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

拥有回忆 提交于 2019-11-27 22:28:25
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 Ember code: App.Store = DS.Store.extend revision: 11 adapter: DS.RESTAdapter.create url: '/api' App