has-many-through

Rails has_many through aliasing with source and source_type for multiple types

走远了吗. 提交于 2019-12-03 16:59:23
问题 So here is a sample class class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm' has_many :angels, through: :investments, source: :investor, source_type: 'Person' end @company.angels and @company.vc_firms works as expected. But how would I have @company.investors that are comprised of both source types? That would work for all polymorphics on the investor column of the Investments table? or perhaps a

Saving the order of associated records in a Rails has_many :through association

試著忘記壹切 提交于 2019-12-03 14:07:09
I'm working on a Rails plugin that includes a way to modify the order of associated records in a has_many :through association. Say we have the following models: class Playlist < ActiveRecord::Base has_many :playlists_songs, :dependent => :destroy has_many :songs, :through => :playlists_songs end class Song < ActiveRecord::Base has_many :playlists_songs, :dependent => :destroy has_many :playlists, :through => :playlists_songs end class PlaylistsSong < ActiveRecord::Base belongs_to :playlist belongs_to :song end If we change the order of a Playlist's Songs (e.g. @playlist.songs.rotate! ), Rails

has_many :through + polymorphic relationships

本小妞迷上赌 提交于 2019-12-03 10:14:10
问题 I using rails3 and trying to build some complex associations. I have Product, Version and Property models. class Version < ActiveRecord::Base belongs_to :product has_many :specs has_many :properties, :through => :specs end class Product < ActiveRecord::Base has_many :versions has_many :specs has_many :properties, :through => :specs end class Property < ActiveRecord::Base end class Spec < ActiveRecord::Base belongs_to :product belongs_to :spec belongs_to :version end It works perfect, but i

Nested Simple Form in Rails4 - has many through, save multiple records

巧了我就是萌 提交于 2019-12-03 08:48:05
I've got a standard has_many through relationship. Humans have many Orcs through a join table Interactions. The interactions is just a table and model; no controller or views. Using the simpleform gem in Rails 4, I want to make a form from the humans page, in order to select multiple orcs out of the pool of all orcs. Once submitted, I want it to create/update as many records in the interactions table, each with the human id, and as many orc ids were selected. : AKA list notation Make a form from one end (humans) List out all the orcs in the form Select multiple orcs from that list Save as many

Filtering child objects in a has_many :through relationship in Rails 3

泪湿孤枕 提交于 2019-12-03 07:08:34
问题 Greetings, I have an application where Companies and Users need to belong to each other through a CompanyMembership model, which contains extra information about the membership (specifically, whether or not the User is an admin of the company, via a boolean value admin ). A simple version of the code: class CompanyMembership < ActiveRecord::Base belongs_to :company belongs_to :user end class Company < ActiveRecord::Base has_many :company_memberships has_many :users, :through => :company

multiple database connections with has_many through

给你一囗甜甜゛ 提交于 2019-12-03 07:00:06
How can I make a has_many through work with multiple database connections? I have a database named "master" that holds the location information. That is updated from a separate application. Users can have access to many locations, but all the other models are located in another database named "budget". Here are how the models are setup. # place.rb class Place < ActiveRecord::Base belongs_to :user belongs_to :location end # user.rb class User < ActiveRecord::Base has_many :locations, :through => :places has_many :places end # location.rb class Location < ActiveRecord::Base establish_connection

Rails has_many through aliasing with source and source_type for multiple types

核能气质少年 提交于 2019-12-03 06:51:58
So here is a sample class class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm' has_many :angels, through: :investments, source: :investor, source_type: 'Person' end @company.angels and @company.vc_firms works as expected. But how would I have @company.investors that are comprised of both source types? That would work for all polymorphics on the investor column of the Investments table? or perhaps a way of using a scope to merge all source_type? Investment model looks like this: class Investment <

rspec testing has_many :through and after_save

耗尽温柔 提交于 2019-12-03 03:41:39
I have an (I think) relatively straightforward has_many :through relationship with a join table: class User < ActiveRecord::Base has_many :user_following_thing_relationships has_many :things, :through => :user_following_thing_relationships end class Thing < ActiveRecord::Base has_many :user_following_thing_relationships has_many :followers, :through => :user_following_thing_relationships, :source => :user end class UserFollowingThingRelationship < ActiveRecord::Base belongs_to :thing belongs_to :user end And these rspec tests (I know these are not necessarily good tests, these are just to

HMT collection_singular_ids= deletion of join models is direct, no destroy callbacks are triggered

坚强是说给别人听的谎言 提交于 2019-12-03 03:02:15
Just ran into an issue with a has_many :through association and after/before-destroy callbacks not being triggered. Say, I have users, groups, and an intermediate relation called membership. I have a form that allows users to be enrolled into groups by creating a new membership record when they check off associated checkboxes. Basically an array of group_ids. Looks something like this: Which group would you like to join? (check all that apply) [] Group A [] Group B [] Group C And I wish to record actions such as joining a group or leaving a group to activity log table and do some other less

has_many :through uninitialized constant

a 夏天 提交于 2019-12-03 02:49:24
I've read the documentations and tons of tutorials about the has_many :through relations in Rails but I can't for the life of me get the hang of it. I'm trying to add a group to my current_user(devise) and I have a table in between Group and User with a status(The user's status is changeable for that group). Whenever I create a new Group now I get an error saying uninitialized constant Group::GroupUser here are my models: groupuser.rb class GroupUser < ActiveRecord::Base belongs_to :group belongs_to :user end group.rb class Group < ActiveRecord::Base has_many :clients has_and_belongs_to_many