has-many-through

Rails Collection_Select Has_Many Through

て烟熏妆下的殇ゞ 提交于 2021-02-08 06:13:22
问题 I have a user that has many accounts. I want to use a collection_select to have the user select which account they want to use. The select need to select among all the accounts assigned to the user in the user_accounts table, but the select needs to check the accounts table to get the name of the account that the drop down menu needs to display. #user.rb class Account < ActiveRecord::Base cattr_accessor :current_id belongs_to :owner, class_name: 'User' has_many :user_accounts has_many :users,

Rails Collection_Select Has_Many Through

旧巷老猫 提交于 2021-02-08 06:03:33
问题 I have a user that has many accounts. I want to use a collection_select to have the user select which account they want to use. The select need to select among all the accounts assigned to the user in the user_accounts table, but the select needs to check the accounts table to get the name of the account that the drop down menu needs to display. #user.rb class Account < ActiveRecord::Base cattr_accessor :current_id belongs_to :owner, class_name: 'User' has_many :user_accounts has_many :users,

has_many :through and collection_select rails form

删除回忆录丶 提交于 2021-01-27 20:25:53
问题 I have tried all of the solutions to similar problems and haven't gotten this one figured out. I have a has_many :through relationship between 'Clinician', and 'Patient' with a joined model 'CareGroupAssignment'. None of the methods I have tried so far been able to save the clinician to patient association. I would like to have a patient be able to have multiple clinicians associated with it and clinicians will have multiple patients . clinician.rb (simplified) class Clinician < ActiveRecord:

Alternative for Model.all - Model.where(condition)

我们两清 提交于 2020-04-30 07:09:04
问题 My question is quiet simple: How to optimize the following query to not get performance issues once the database is growing: def projects_not_participating @project = Project.all - Project.joins(:participants).where(participant: {id: current_user.id}) end My model setup is this: def Project has_many :assignments has_many :participants, through: :assignments end def Participant has_many :assignments has_many :projects, through: assignments end I tried using Project.joins(:participant).where

find_or_create on a has many through relationship

孤街醉人 提交于 2020-01-24 02:26:11
问题 I have a has many through relationship in my app: Shows has many Bands through => Lineups Bands are unique by :name class Show < ActiveRecord::Base attr_accessible :city_id, :title, :dateonly, :timeonly, :image, :canceled, :venue_attributes, :bands_attributes belongs_to :city belongs_to :venue has_many :lineups has_many :bands, through: :lineups has_and_belongs_to_many :users end class Lineup < ActiveRecord::Base belongs_to :show belongs_to :band end class Band < ActiveRecord::Base attr

Rails: ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s)

China☆狼群 提交于 2020-01-21 02:49:27
问题 I have the following code (somewhat simplified ... create_table :signatures do |t| t.integer :signer_id t.integer :card_id t.timestamps end With the models looking like ... class Signature < ActiveRecord::Base belongs_to :card belongs_to :user end class Card < ActiveRecord::Base has_many :signatures has_many :signers, :through => :signatures, :foreign_key => "card_id" end class User < ActiveRecord::Base has_many :sent_cards, :class_name => "Card", :foreign_key => "sender_id" has_many

Rails - using group_by and has_many :through and trying to access join table attributes

笑着哭i 提交于 2020-01-17 02:23:04
问题 For some background, please see my question: Rails app using STI -- easiest way to pull these records? I have some models joined with a has_many through relationship using a third join table (Recipe and Ingredient joined through RecItem). So the guys on the prior question helped me to group my recipe's ingredients by type - this is exactly what I wanted. However, now that I am accessing @recipe.ingredients instead of @recipe.rec_items, I can't get back to the data stored in the rec_items join

Rails - using group_by and has_many :through and trying to access join table attributes

血红的双手。 提交于 2020-01-17 02:23:02
问题 For some background, please see my question: Rails app using STI -- easiest way to pull these records? I have some models joined with a has_many through relationship using a third join table (Recipe and Ingredient joined through RecItem). So the guys on the prior question helped me to group my recipe's ingredients by type - this is exactly what I wanted. However, now that I am accessing @recipe.ingredients instead of @recipe.rec_items, I can't get back to the data stored in the rec_items join

Order by count of has_many :through items in Rails 3

时光怂恿深爱的人放手 提交于 2020-01-16 04:42:09
问题 If I have 3 models: Model Section Model User has_many :votes Model Vote belongs_to :user and inside Model Section has_many :users has_many :votes, :through => :users How to get the Sections list ordered by votes quantity using the AR associations? 回答1: The most reasonable way to do this is to use a subquery written as raw SQL for ordering the result as follows... Section.order( '(select count(1) from votes inner join users on votes.user_id=users.id where users.section_id=sections.id)' ) 回答2:

The “create new entry” page on a many-to-many-relationship

岁酱吖の 提交于 2020-01-14 06:22:13
问题 I am a new rails user. This is probably a very basic question, but I need help I am making an app that has a Bands table, a Festivals table, and a Participations table in a many-to many relationship. My models are as follows: class Band < ActiveRecord::Base attr_accessible :year, :genere, :name, :country has_many :participations has_many :festivals, :though => :participations end class Festival < ActiveRecord::Base attr_accessible :city, :name, :country has_many :participations has_many