has-many-through

Correct Routing for :has_many :through

I am trying to set up a Many-to-Many association between 2 objects. I have gone through several tutorials and have been able to correctly set up the model. My problem is that I am having having trouble setting up the correct routes so I can view the full relationship... something like only displaying the products from a specific catego...

accepts_nested_attributes_for with has_many => :through Options

I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model. Associations: class Link < ActiveRecord::Base has_many :tags, :through => :link_tags has_many :link_tags accepts_nested_attributes_for :tags, :allow_destroy => :false, :reject_if => proc { |attrs| attrs.all? { |k...

has_many :through ,self referential association

I have trouble with the self referential association, the models should give ma an array of models for the left_chunks and right_chunks methods, but I get everytime an empty array The source class Chunk < ActiveRecord::Base has_many :left_bindings, :foreign_key => "left_chunk_id", :class_name => "ChunkChunk", :dependent => :des...

Accessing values in a has_many :through join table

I've got users who are members of groups through a membership join table, and one of the attributes of that join table is "administrator". I'm trying to do a check inside of a group's member view, looping through each member to see if they are an administrator. In the view I tried the following: for user in @group.users if user.admin...

How to access the joining model on a has_many :through relationship

I have a typical many-to-many relationship using has_many => :through, as detailed below. class member has_many member_roles has_many roles, :through => :member_roles end class role has_many member_roles has_man members, :through => :member_roles end class member_role belongs_to :member belongs_to :role # has following f...

Eliminate the Join Table in HasMany Relationships in Rails

I am thinking about ways to create a Role Based Access Control system in Rails. I have seen these great projects too (among others): RoleRequirement ActsAsPermissible RailsAuthorization BoxRoom (rails file management) My question is, is it really necessary to have a join table for everything? If I one of the tables in the relations...

How do I link several elements of different types to one another in RoR?

I have Events, Documents, and Surveys which all need to be able to link to one another, I was planning on having a linking table with four columns as follows: link_elements{ element1_type CHAR(1) element1_id INTEGER element2_type CHAR(1) element2_id INTEGER } The problem is I can't figure out how to make the model in RoR so th...

aggregate conditional has_many rails association

I'm not sure if this is feasible or even preferable But I'd like to build a has_many relationship or a conditional named_scope type relationship to ease my pain into one simple relationship instead of two. Currently I have two models which basically contain the same information and belong to the same Model rooms thru the same key room_...

Ruby-on-Rails: Multiple has_many :through possible?

Is it possible to have multiple has_many :through relationships that pass through each other in Rails? I received the suggestion to do so as a solution for another question I posted, but have been unable to get it to work. Friends are a cyclic association through a join table. The goal is to create a has_many :through for friends_comm...

has_many :through and FormBuilder.fields_for

I have a class Bar that has a user-defined list of config keys and values, defined like this: class Bar < ActiveRecord::Base has_many :config_keys, :through => Foo has_many :config_values end So the available config keys come from the Foo class and the values for those come from the Bar class. I'm creating a form for this Bar...

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

Hi! 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...

rails: has_many :through validation?

Rails 2.1.0 (Cannot upgrade for now due to several constraints) I am trying to achieve this. Any hints? A project has many users through join model A user has many projects through join model Admin class inherits User class. It also has some Admin specific stuff. Admin like inheritance for Supervisor and Operator Project has one Admin,...

Should this even be a has_many :through association?

A Post belongs_to a User, and a User has_many Posts. A Post also belongs_to a Topic, and a Topic has_many Posts. class User < ActiveRecord::Base has_many :posts end class Topic < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user belongs_to :topic end Well, that's pretty simple and very...

MySQL: 4 Table "has-many-through" Join?

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 apple...

Is it possible to implement has_many, :through with ActiveScaffold?

Consider the following models: class Artist < ActiveRecord::Base has_many :artist_events has_many :events, :through => :artist_events end class Event < ActiveRecord::Base has_many :artist_events has_many :artists, :through => :artist_events, :order => 'artist_events.position' end class ArtistEvent < ActiveRecord::Base defaul...

How do I do a has_many through association on a has_many through association in rails?

Warning: I may have the wrong 'problem statement' but here it goes: A Campaign has many Contacts. A Campaign has many Emails. Therefore, a Contact has many Emails through a Campaign. And an Email can have many Contacts through a Campaign. Each Contact-Email pair has its own unique Status (status1, status2, etc). Each Status (for a ...

has_many relation doesn't seems right or logical in business perceptive, needed some thing like belongs_to_many?

My situation is like this. Company has many users and users may belongs to many companies. And current implementation is something like below. class Company has_many :employments has_many :users, :through => :employments end class Employment belongs_to :company belongs_to :user end class User has_m...

How do I use has_many :through and in_place_edit?

I have two Models: Campaign and Contact. A Campaign has_many Contacts. A Contact has_many Campaigns. Currently, each Contact has a contact.date_entered attribute. A Campaign uses that date as the ate to count down to the different Events that belong_to the Campaign. However, there are situations where a Campaign for a specific Conta...

Using named_scopes on the join model of a has_many :through

Hi folks. I've been beating my head against the wall on something that on the surface should be very simple. Lets say I have the following simplified models: user.rb has_many :memberships has_many :groups, :through => :memberships membership.rb belongs_to :group belongs_to :user STATUS_CODES = {:admin => 1, :member => 2, :invi...

How do I use accepts_nested_attributes_for?

Editing my question for conciseness and to update what I've done: How do I model having multiple Addresses for a Company and assign a single Address to a Contact, and be able to assign them when creating or editing a Contact? I want to use nested attributes to be able to add an address at the time of creating a new contact. That addre...