has-many-through

Rails has_many :through Find by Extra Attributes in Join Model

New to both Ruby and Rails but I'm book educated by now (which apparently means nothing, haha). I've got two models, Event and User joined through a table EventUser class User < ActiveRecord::Base has_many :event_users has_many :events, :through => :event_users end class EventUser < ActiveRecord::Base belongs_to :event belongs...

Elegantly selecting attributes from has_many :through join models in Rails

I'm wondering what the easiest/most elegant way of selecting attributes from join models in has_many :through associations is. Lets say we have Items, Catalogs, and CatalogItems with the following Item class: class Item < ActiveRecord::Base has_many :catalog_items has_many :catalogs, :through => :catalog_items end Additio...

Destroy associations after the last has_many :through record is deleted

With a regular has_many, there's the option of :dependent => :destroy to delete the associations when the parent record is deleted. With has_many :through, there might be other parents associated to the child records, so :dependent => :destroy doesn't have any effect. How do you ensure child records are deleted after they are orphaned f...

has_many :through breaks upon find_or_initialize_by

Hello, I have a model, Feed, that has and belongs to many FilteredUsers. In this case I have implemented it through a has_many :through relationship. class Feed < ActiveRecord::Base has_many :denials, :dependent => :destroy has_many :filtered_users, :through => :denials I would like to create a record if it doesn't exist or find t...

Using build with has_many :through

I have an Entry model and a Category model, where an Entry can have many Categories (through EntryCategories): class Entry < ActiveRecord::Base belongs_to :journal has_many :entry_categories has_many :categories, :through => :entry_categories end class Category < ActiveRecord::Base has_many :entry_categories, :dependent => :de...

has_many through update issue

Hi there, Been trying to sort this for over a day now, and I am sure that it is something simple that I am missing. I have a project, which can have one main category and two optional categories. My relevant code for the project model: has_many :project_categories has_one :optional_category_1, :through => :project_cate...

What is the default code for bulk has_many :through join assignment in rails?

Hi there, I have a basic has_many :through relationship that is bi-directional: calendars have many calendar_calendar_events calendars have many events through calendar_calendar_events events have many calendar_calendar_events events have many calendars through calendar_calendar_events I'm wanting to assign calendars to an event wit...

using join model attributes in rails has_many :through

as an example, i have a model Group and model User they are connected with :has_many, :through => groups_users groups_users table has an attribute, called moderator, specifying whether a user is a moderator of the group question: how do i access all moderators of a given group? after reading about :with_scope, what comes to mind is ...

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 join module attribute, i get a mistake way i am doing it now: @group_membership=@group...

dependent => destroy on a "has_many through" association

Apparently dependent => destroy is ignored when also using the :through option. So I have this... class Comment < ActiveRecord::Base has_many :comment_users, :dependent => :destroy has_many :users, :through => :comment_users ... end ...but deleting a Comment does not result in the associated comment_user records getting deleted...

Ruby on Rails Has Many Through Array Problem

Hi all, I'm having problems adding to a Has Many Through association using user_ids. My communication model looks like this: class communication has_many :recipients has_many :users, :through => :recipients end In my create action for the communication controller I am trying to manually add user_ids to the communication object lik...

How to save an associating record between has-many classes in Ruby on Rails

I've created three classes to represent Books, People, and BookLoans. While I am able to show the association of People to Books through BookLoans I've been seeding my database. I now need to save a checkout of a book. It was my intention to do this action through the book controller. Specifically, creating a loan action in the BooksCon...

Showing join table field

Here I want to show the extra attribute CONFIRMED from the employments join table. What am I doing wrong? class Job < ActiveRecord::Base has_many :employments, :dependent => :destroy has_many :users, :through => :employments class User < ActiveRecord::Base has_many :employments has_many :jobs, :through => :employments class Empl...

How can I create new records with has_many :through and honor :conditions?

Let's say I have a Course in which Students can enroll via a Membership (e.g. a has_and_belongs_to_many relationsip of Courses and Students). Some memberships are for students who are just observing the class (not for credit, etc.), so: class Course < ActiveRecord::Base has_many :memberships has_many :students, :through...

Updating join table field

class Job < ActiveRecord::Base has_many :employments, :dependent => :destroy has_many :users, :through => :employments class User < ActiveRecord::Base has_many :employments has_many :jobs, :through => :employments class Employment < ActiveRecord::Base belongs_to :job belongs_to :user # Employment has an extra attribute of confirm...

How do I define ActiveRecord relationships between two models that relate to each other in two different ways?

In my app I have the classes User, Video, and Vote. Users and Videos can relate to each other in two different ways: as a one-to-many or as a many-to-many. The former is when a User submits a Video (one user can submit many videos). The latter is when a user votes on a video (users have many videos through votes, and vice versa). H...

has_many through self referential association

I want to (as an example) create a has_many association to all posts by friends of a person, something like has_many :remote_posts to give me something like person > friends > person > posts. ..here is how I would go about it script/generate model post title:string person_id:integer script/generate model friendship person_id:integer f...

How to provide has_many through association through 3 models?

I hope someone has already experienced this. Please help me, how can i solve this problem: class Article < ActiveRecord::Base belongs_to :author belongs_to :publisher has_one :address, :through => :publisher end class Author < ActiveRecord::Base has_many :articles has_many :addresses, :through => :articles, :source => :addres...

has_many :through questions...

I was previously using has_and_belongs_to_many, and have converted to has_many :through. Here's how it looks for a list of games that can have many users playing. With this, I can do game.users and user.games....: class Game < ActiveRecord::Base has_many :game_users, :dependent => :destroy has_many :users, :through => :game_users, :...

Rails creating users, roles, and projects

I am still fairly new to rails and activerecord, so please excuse any oversights. I have 3 models that I'm trying to tie together (and a 4th to actually do the tying) to create a permission scheme using user-defined roles. class User < ActiveRecord::Base has_many :user_projects has_many :projects, :through => :user_projects has_m...