has-many-through

Distant HasManyThrough

大憨熊 提交于 2019-12-24 03:44:52
问题 I have four Models: User Client Store Opportunity The relationships are defined as such: User hasMany Client Client hasMany Store Store hasMany Opportunity User hasManyThrough Store, Client (this works) The problem is that I'm attempting to access the User->Opportunity relationship via built-in Laravel relationships, but it doesn't seem as if I can do it without a custom Query or an additional user_id column on the opportunities table to allow direct access (even though one can be inferred

Using fields from an association (has_many) model with formtastic in rails

冷暖自知 提交于 2019-12-23 07:38:18
问题 I searched and tried a lot, but I can't accomplish it as I want.. so here's my problem. class Moving < ActiveRecord::Base has_many :movingresources, :dependent => :destroy has_many :resources, :through => :movingresources end class Movingresource < ActiveRecord::Base belongs_to :moving belongs_to :resource end class Resource < ActiveRecord::Base has_many :movingresources has_many :movings, :through => :movingresources end Movingresources contains additional fields, like quantity . We're

has_many through middle model not creating, but creating duplicate of one model

雨燕双飞 提交于 2019-12-23 05:21:02
问题 I have two models join by a middle model class Integration < ActiveRecord::Base has_many :integration_records has_many :records, through: :integration_records end class IntegrationRecord < ActiveRecord::Base belongs_to :integration belongs_to :record end class Record < ActiveRecord::Base has_many :integration_records has_many :integrations, through: :integration_records end i = Integration.create(whatever) i.records.create(whatever) => (0.1ms) BEGIN SQL (8.7ms) INSERT INTO "records" ("created

Rails ActiveRecord: Three Table has_many through: associations

旧城冷巷雨未停 提交于 2019-12-21 23:45:59
问题 I am attempting to build a table to handle both the location and category a certain campaign has been set to with the following model associations: class Campaign < ActiveRecord::Base has_many :campaign_category_metro_bids, dependent: :destroy has_many :metros, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Metro < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids

Simulating has_and_belongs_to_many nested through behavior in Rails 3

让人想犯罪 __ 提交于 2019-12-21 22:20:47
问题 So Rails doesn't have support for :through associations through a habtm relationship. There are plugins out there that will add this in for Rails 2.x, but I'm using Rails 3 / Edge, and only need the association for one particular model. So I thought I'd stump it out myself with the beauty that is Arel. First, the models: class CardSet < ActiveRecord::Base has_and_belongs_to_many :cards, :uniq => true end class Card < ActiveRecord::Base has_and_belongs_to_many :card_sets, :uniq => true has

MySQL: 4 Table “has-many-through” Join?

本小妞迷上赌 提交于 2019-12-21 18:16:23
问题 Let's say I have the following 4 tables (for examples' sake): Owners, Trucks, Boxes, Apples. An owner can have many trucks, a truck can have many boxes and a box can have many apples. Owners have an id. Trucks have an id and owner_id. Boxes have an id and truck_id. Apples have an id and box_id. Let's say I want to get all the apples "owned" by an owner with id = 34. So I want to get all the apples that are in boxes that are in trucks that owner 34 owns. There is a "hierarchy" if you will of 4

ActiveRecord has_many :through duplicating counter caches on mass assignment

情到浓时终转凉″ 提交于 2019-12-21 12:00:51
问题 It seems ActiveRecord's counter_cache feature can result in a counter cache being incremented twice. The scenario in which I am seeing this behavior is when I have two models that have a has_many :through relationship with each other through a join model (ie: Teacher has many Student through Classroom ). When using the has_many :through generated methods to associate Teacher and Student directly (without manually creating the join record) the count goes up 2 times. Example: teacher.students <

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

戏子无情 提交于 2019-12-21 02:53:10
问题 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

multiple database connections with has_many through

人走茶凉 提交于 2019-12-21 01:44:10
问题 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

Rails: Why “has_many …, :through => …” association results in “NameError: uninitialized constant …”

北城以北 提交于 2019-12-20 18:03:11
问题 To express that a group can have multiple users, and a user can belong to multiple groups, I set the following associations: class Group < ActiveRecord::Base has_many :users_groups has_many :users, :through => :users_groups end class User < ActiveRecord::Base has_many :users_groups has_many :groups, :through => :users_groups end class UsersGroups < ActiveRecord::Base belongs_to :user belongs_to :group end However, when I type: Group.find(1).users I get: NameError: uninitialized constant Group