Questions tagged [has-many-through]

1523 questions
4
votes
1 answer

How can I use accepts_nested_attributes_for to create > 1 related object in a join table?

I have a very simple design at present, created using Ruby 2.0 and the Rails 4 beta. I have users, and groups. A User can be a member of multiple groups, and a Group can have multiple users as members. I thus have a join model, UserGroup, which only…
4
votes
1 answer

How to create a nested form for a has_many through association using simple_form and nested form?

I have a rails app that has an album and song model with a has many through relationship. I'm trying to add songs to albums using the simple_form and nested_form gems. If I use simple_form, it's easy to create the association, but I'm having trouble…
4
votes
2 answers

Does has_many association attribute setter always persist its value to a database and is it possible to make it not to?

I have the following script which demonstrates me that has_many roles= attribute always works in a persistent manner. My questions are: 1) What is the reason behind this behavior: why has_many attributes are persisted right at the moment when…
4
votes
2 answers

Rails: nesting same resource in multiple nested restful routes?

This is pretty easy but switching back n' forth between frameworks/languages has me doubting myself. Trying to work out routes for has_many :through relationship in Rails. #User has_many :reservations, :dependent => :destroy has_many :events,…
Meltemi
  • 37,979
  • 50
  • 195
  • 293
4
votes
4 answers

Rails 3, many-to-many form using accepts_nested_attributes_for, how do I set up correctly?

I have a many-to-many relationship between Recipes and Ingredients. I am trying to build a form that allows me to add an ingredient to a recipe. (Variants of this question have been asked repeatedly, I have spent hours on this, but am fundamentally…
4
votes
2 answers

rails 3 has_many through has_one

Suppose you have the following models: class Category < ActiveRecord::Base has_one :current_heat, class_name: 'Heat' has_many :scores, :through => :current_heat end class Heat < ActiveRecord::Base belongs_to :category has_many…
mgadda
  • 1,429
  • 12
  • 13
4
votes
3 answers

Rails has_many through with condition, build new

I've got users and organisations with a join model UsersOrganisation. Users may be admins of Organisations - if so the is_admin boolean is true. If I set the is_admin boolean by hand in the database, Organisations.admins works as I'd expect. In the…
Edward
  • 3,429
  • 2
  • 27
  • 43
4
votes
2 answers

Good name for a has_many through association

I've read about many to many relationship in rails 3 and saw that HABTM has been "deprecated", as in one should use has_many :through most of the time. I saw plenty of examples where the Join Model has a clear name, for example Magazine, Suscriber =…
Pontek
  • 149
  • 11
3
votes
1 answer

Adding attribute values to a join table record in a has_many :through association upon creation

I am setting up a has_many :through relationship on my Rails 3.2 app. I have most everything working, except that I'm not sure how to add values to attributes on the join table when relationships are created. Here are the models (notice the…
3
votes
1 answer

Nested :has_many, :through attributes

I am using a has_many :through to create an association between two models using a pass-through called  list_items : user.rb: has_many :list_items has_many :wishes, :through => :list_items wishe.rb: has_many :list_items has_many :users,…
Jayson Lane
  • 2,828
  • 1
  • 24
  • 39
3
votes
1 answer

has_many :through query with both values

Let's say I have: class Post has_many :tags, :through => :taggings has_many :taggings end Notice there's no :include. Now say I want to retrieve all taggings and tags in the same query. How could I do that? I'm looking for something…
mahemoff
  • 44,526
  • 36
  • 160
  • 222
3
votes
2 answers

acts_as_tree and has_many :through not working well together

I have the following: class Menu < ActiveRecord::Base has_many :menu_headers # has_many :menu_headers, :conditions => {:parent_id => 0} - was trying # to set parent_id to 0 for top level has_many :menu_items, :through =>…
3
votes
1 answer

Rails / Active Record has_many through association - fetching a record

I have a has_many relationship of models Role and Access through model Permission. I have a situation that no two roles should have identical accesses. So, I created a custom validation which basically results in an error message when current…
3
votes
1 answer

Duplicate records when using update_attributes() and a has_many :through association

I can't figure out why it's generating duplicate recruit_profiles_skills instead of updating. class RecruitProfile < ActiveRecord::Base has_many :skills, :through => :recruit_profiles_skills has_many :recruit_profiles_skills, :dependent =>…
hrdwdmrbl
  • 4,814
  • 2
  • 32
  • 41
3
votes
2 answers

How do I set a join model's attribute on the fly when creating a relationship?

I have a has_many :through relationship between users and projects via an ownership join model. I want to be able to set an attribute of the ownership model while creating a relationship between a user and a new project. Here is what I have so…
Matthew Berman
  • 8,481
  • 10
  • 49
  • 98