has-many-through

Using HABTM or Has_many through with Active Admin

安稳与你 提交于 2019-12-06 00:48:33
I've read quite a few of the posts on using active admin with has_many through association but I'm not getting the desired results. Essentially I have 2 models "Conferences" & "Accounts". I need to assign multiple accounts to a conference and multiple conferences to an account. Wether I use HABTM or has_many through doesn't matter to me. I just need to be able to see a dropdown select option when I create a new conference and vice versa on accounts. Account Model class Account < ActiveRecord::Base attr_accessible :address, :city, :name, :phone, :state, :website, :zip has_many :contacts,

Rails has_many :through with conditions and building associations

半腔热情 提交于 2019-12-05 23:47:45
问题 I'm having problems building an association that is a has_many :through with conditions. I have this model: class Contact < AR has_many :group_contacts has_many :groups, :through => :group_contacts, :conditions => {:groups => {:published => true}} end problem happens when I try to instantiate a group from a contact. With the above syntax, I get an error: contact.groups.build => ActiveRecord::UnknownAttributeError: unknown attribute: groups But when I use the following syntax it works: has

Rails has_many through polymorphic counter cache

心已入冬 提交于 2019-12-05 05:19:19
问题 I have two models I link together using a polymorphic has_many through association and I would like to add a counter_cache but it seems Rails/ActiveRecord does not support this feature out of the box. class Classifiable < ActiveRecord::Base has_many :classifications, :as => :classifiable, :foreign_key => :classifiable_id end class Taxonomy < ActiveRecord::Base has_many :classifications, :as => :taxonomy, :foreign_key => :taxonomy_id end class Question < Classifiable has_many :categories,

How do I create an association with a has_many :through relationship in Factory Girl?

跟風遠走 提交于 2019-12-05 04:50:15
问题 In my models I have the following setup: class User < ActiveRecord::Base has_many :assignments has_many :roles, :through => :assignments end class Role < ActiveRecord::Base has_many :assignments has_many :users, :through => :assignments end class Assignment < ActiveRecord::Base belongs_to :user belongs_to :role attr_accessible :role_id, :user_id end In my factory.rb file I have: FactoryGirl.define do factory :user do sequence(:username) { |n| "user#{n}" } email { "#{username}@example.com" }

Has_Many :Through or :finder_sql

▼魔方 西西 提交于 2019-12-05 02:52:00
I've nailed down what I want, but I can't seem to get it in a way that the rails designers are looking for. Basically, I have (please set aside pluralization/etc issues): Human Relationships (Parent, Offspring) I'm trying to get all the offsprings for a single parent, and the single parent for many offsprings (assume only one parent per offspring). I can do this in the following way in the model: has_one :parent, :through => :relationships, :foreign_key => :human_id, :source => :source_human has_many :offsprings, :finder_sql => 'SELECT DISTINCT offsprings.* ' + 'FROM humans offsprings INNER

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

北慕城南 提交于 2019-12-04 21:35:16
问题 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

User has_many :users, :through => :friends - how?

风流意气都作罢 提交于 2019-12-04 20:59:45
This is my code: class Friend < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => "User", :foreign_key => "friend_id" end class User < ActiveRecord::Base #... has_many :friends has_many :users, :through => :friends #... end When I now start adding users by... user.users << user2 user.save Only the user_id of friend is filled, friend_id is null. Any help? Yours, Joern. You need to add the :source attribute to your has_many through association. class User < ActiveRecord::Base has_many :friends has_many :users, :source => :friend, :through => :friends end Now the following

How to find records, whose has_many through objects include all objects of some list?

谁说胖子不能爱 提交于 2019-12-04 19:42:58
I got a typical tag and whatever-object relation: say class Tag < ActiveRecord::Base attr_accessible :name has_many :tagazations has_many :projects, :through => :tagazations end class Tagazation < ActiveRecord::Base belongs_to :project belongs_to :tag validates :tag_id, :uniqueness => { :scope => :project_id } end class Project < ActiveRecord::Base has_many :tagazations has_many :tags, :through => :tagazations end nothing special here: each project is tagged by one or multiple tags. The app has a feature of search: you can select the certain tags and my app should show you all projects which

Rails: has_many with extra details?

喜欢而已 提交于 2019-12-04 19:41:12
While I'm not a complete Ruby/Rails newb, I'm still pretty green and I'm trying to figure out how to structure some model relationships. The simplest example I can think of is the idea of "recipes" for cooking. A recipe consists of one or more ingredients and the associated quantity of each ingredient. Assume we have a master list in the database of all ingredients. That suggests two simple models: class Ingredient < ActiveRecord::Base # ingredient name, end class Recipe < ActiveRecord::Base # recipe name, etc. end If we just wanted to associate Recipes with Ingredients, that's as simpling as

Rails associations: How do I limit/scope a has_many :through with multiple self-referencing conditions?

这一生的挚爱 提交于 2019-12-04 19:40:56
What's the best way to do this? I'm trying something like this, but it feels... wrong. (NOTE: It's all about the :conditions in the has_many association...) class Submission < ActiveRecord::Base belongs_to :checklist has_many :jobs, :through => :checklist, :source => :job, :conditions => ["jobs.archived = false OR jobs.archived_at > #{self.created_at} OR jobs.created_at < #{self.created_at}"] end Rails 3.2.11, Ruby 1.9.3 What I'm trying to do I need to pass multiple conditions on a has_many :through association and refer to the "self" model to do comparisons in those conditions. I've found