Questions tagged [has-many-through]

1523 questions
0
votes
1 answer

Duplicate nested associations returned from json POST

I'm attempting use the REST API in my rails (version 3.2.9) app. I am POSTing a json packet to a standard rails scaffolded controller, which includes details for the model itself, and nested children. The parent model and its children are…
0
votes
1 answer

How to write has_many through two objects (to the same 'destination' object)?

What is the correct way to describe the relationship between a User and the Outcomes of their Questions and Contacts? I want to be able to call User.outcomes and get all outcomes for the user, regardless of whether the outcome was for a question or…
kwh941
  • 235
  • 2
  • 9
0
votes
1 answer

has_many relationship rails 3

Hi I have three tables like the following: class Workitem < ActiveRecord::Base has_many :effort attr_protected end class Effort < ActiveRecord::Base attr_protected belongs_to :workitem belongs_to :person …
0
votes
2 answers

Yii Many To Many To Many?

Let's say I have 3 main tables: category, tag, url category and tag have a MANY_MANY relationship. tag and url have a MANY_MANY relationship. I would like from category to retrieve all the linked url. What is the easiest, most efficient way to do…
Nathan H
  • 48,033
  • 60
  • 165
  • 247
0
votes
1 answer

Associate Model and Update Attribute

I have 2 models with a through relationship: PortfolioItem has_many :portfolio_item_images has_many :images, :through => :portfolio_item_images PortfolioItemImage belongs_to :image belongs_to :portfolio_item I want to associate a portfolio item…
Zhao Li
  • 4,936
  • 8
  • 33
  • 51
0
votes
1 answer

rails 3 eager load has_many through association with limit

I'm trying to limit a has_many through association for certain instances where I want to limit the number of results when using .includes(:model) Here's what I have so far: class Student < ActiveRecord::Base has_many :courses, :through =>…
agmin
  • 8,948
  • 2
  • 21
  • 27
0
votes
1 answer

Rails has_many_through form

After a number of hours reading stackoverflow and watching railscasts, I've decided to post. The question is very similar if not identical to many other questions here but I'm just not getting it. This is my first has_many_through association. class…
Clay
  • 162
  • 2
  • 9
0
votes
1 answer

Automatically create associations through many existing associations

I'm working on an engine where any model can have a has_many association with Permit as Permissible: class Permit < ActiveRecord::Base belongs_to :permissible, polymorphic: true end module Permissible def self.included(base) base.class_eval…
0
votes
1 answer

has_many through association with existing object / ActiveRecord

Given models class Composition < ActiveRecord::Base attr_accessible :content has_many :compositions_tags has_many :tags, :through => :compositions_tags end class Tag < ActiveRecord::Base attr_accessible :text has_many…
neilmarion
  • 2,372
  • 7
  • 21
  • 36
0
votes
1 answer

Why does the Rails Guides say has_many :through implements a many-to-many relationship?

To me it appears a nested one-to-many relationship.
Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90
0
votes
1 answer

How can I update has_many :through join model attribute in Rails?

I have a model Group and model User They are connected both directions with "has_many :through => groups_users" groups_users table has an attribute, called moderator, specifying whether a user is a moderator of the group when i am trying to update…
Pavel K.
  • 6,697
  • 8
  • 49
  • 80
0
votes
1 answer

rails: Create does not work with :through in third model

So I have a nested resource with resources :albums do resources :elements end I can update, delete and view those elements. What I can not really do is to create a new element. So it is actually created in the database but not in the mapping…
user1697061
  • 255
  • 3
  • 14
0
votes
0 answers

How to go through more than one has_many :through?

I need to access an information that is stored 2 relations from the one I'm doing the query on. class Information < ActiveRecord::Base ... belongs_to :information_type, polymorphic: true ... end class InformationTypeOne < ActiveRecord::Base …
0
votes
2 answers

rails has_many :through NameError

I have the following setup class Category < ActiveRecord::Base has_many :category_products has_many :products, through: :category_products end class Product < ActiveRecord::Base has_many :category_products has_many :categories, through:…
Mark Soden
  • 81
  • 9
0
votes
0 answers

Has_many through mass assignment error

I've read through the many threads on this topic and still can't see why my code won't work. I have the following: class User < ActiveRecord::Base has_many :user_roles has_many :user_groups, :through => :user_roles #define attributes and…