has-many-through

accepts_nested_attributes_for and has_many :through relations.

I want to make a simple examing application on RoR 2.3. The problem area is to make an exam_session in a one form with only one submit action. For the exam session there are selected some number of questions from the question pool in random order. For these questions there are selected some number of alternatives (to check is this a sing...

Ruby on Rails: has_many through frustrations

I'm having a frustrating problem with a has_many through: namely the fact that the through models are not created until save. Unfortunately, I need to set data on these models prior to saving the parent. Here's the loose setup: class Wtf < ActiveRecord::Base belongs_to :foo belongs_to :bar end class Bar < ActiveRecord::Base ha...

One way Has-Many-Through

Hello, I have a Category, a Subcategory and a Product model. I have: Category has_many Subcategories Subcategory has_many Products Subcategory belongs_to Category Product belongs_to Subcategory Is there a way to have something like Category has_many Projects through Subcategories ? The 'normal' rails way wouldn't work because "su...

Creating has_many :through records 2x times

I have models class Question < ActiveRecord::Base WEIGHTS = %w(medium hard easy) belongs_to :test has_many :answers, :dependent => :destroy has_many :testing_questions end class Testing < ActiveRecord::Base belongs_to :student, :foreign_key => 'user_id' belongs_to :subtest has_many :testing_questions, :dependent => :dest...

Rails - Logic for finding info from a :has_many :through needed!

I have 3 relevant tables. User, Orders, and Viewables The idea is that each User has got many Orders, but at the same time, each User can View specific other Orders that belong to other Users. So Viewables has the attributes of user_id and order_id. Orders has a :has_many :Users, :through => :viewables Is it possible to do a find thr...

Rails preventing duplicates in polymorphic has_many :through associations

Is there an easy or at least elegant way to prevent duplicate entries in polymorphic has_many through associations? I've got two models, stories and links that can be tagged. I'm making a conscious decision to not use a plugin here. I want to actually understand everything that's going on and not be dependent on someone else's code that...

Rails: bi-directional has_many :through relationship

I have three models in a Rails application: Game represents an instance of a game being played. Player represents an instance of a participant in a game. User represents a registered person who can participate in games. Each Game can have many Players, and each User can have many Players (a single person can participate in multiple game...

Joining a one-to-many association with a many-to-many association in Rails 3

I have a many-to-many association between a User class and a Table class. Additionally, i have a one-to-many association between the User and the Table (one User ultimately owns the table). I am trying to access all of the tables which the user may access (essentially joining both associations). Additionally, it would be nice to do this...

How To Get Additional Attributes From Has Many Through

I am using Rails 3 beta 4. I have the following models: class Player < ActiveRecord::Base has_many :players_items, :dependent => :destroy has_many :items, :through => :players_items end class PlayersItem < ActiveRecord::Base belongs_to :player belongs_to :item end class Item < ActiveRecord::Base has_many :players_items, :d...

:limit number of rows found in a collect (has_many association)

Category has_many :products has_many :deals, :through => :products Product has_many :deals I want to display a limited number of deals on a category page. In categories_helper.rb: def deals @category.products.collect { |c| c.deals}.flatten end In the show.html.erb (Category): <% for deal in deals %> <%= deal.name %> <% en...

Adding a Rating to each User per category on Ruby on Rails

Right now I'm building a social media app, where i want an user to have a rating per category, how would the association go? The way it needs to be setup it's Each user will have a different rating in each category. I'm think that belongs_to :user belongs_to :category in the UserCategoryRating model. and has_many :user_category_r...

has_many :through, forms and validation

I have two quite nasty problems with join model. I think I am just going wrong way, so maybe someone can clear up my approch a bit. First there are some models with realtions like this: class Account < ActiveRecord::Base has_many :tickets, :through => :assigment has_many :assigments ... end accepts_nested_attributes_for :assigmen...

Simulating has_and_belongs_to_many nested through behavior in Rails 3

So Rails doesn't have support for :through associations through a habtm relationship. There are plugins out there that will add this in for Rails 2.x, but I'm using Rails 3 / Edge, and only need the association for one particular model. So I thought I'd stump it out myself with the beauty that is Arel. First, the models: class CardSet ...

Kohana "has many through" relation

Hi, I was wondering what the best method is to edit a 'has many through' relation with a form. Let's say I have a bunch of users that can belong to multiple categories. The form would have some checkboxes like this: <input type="checkbox" name="category_ids" value="1" /> <input type="checkbox" name="category_ids" value="2" /> Th...

rails :dependent=>:destroy being ignored.

My association option does not appear to be honored. class ClassRoom < ActiveRecord::Base has_many :class_assignments, :dependent => :destroy has_many :people, :through=>:class_assignments class Person < ActiveRecord::Base has_many :class_assignments, :dependent => :destroy has_many :class_rooms, :through=>:class_assignments c...

How to access ':has_many :though' join table data when using to_json?

Hi folks, I have three models (simplified here): class Child < ActiveRecord::Base has_many :childviews, :dependent => :nullify has_many :observations, :through => :childviews end class Childview < ActiveRecord::Base belongs_to :observation belongs_to :child end class Observation < ActiveRecord::Base has_many :chi...

converting has_many and :belongs_to with has_many :through

I have a current association where: Group :has_many Employees and Employee :belongs_to Group but now I want an Employee to be associated to many Groups as well. For this purpose I am thinking of making: groupizations group_id:integer employee_id:integer created_at:datetime This will change Employee and Group models: class Groupiza...

Multiple has_many association through same table in Rails

I have following database schema: I want to be able to do something like this: dog.head << Feature.new(...) dog.tail << Feature.new(...) I am new to Rails, so I am not always sure by 100% what I am writing, but I tried following declaration of Dog class, and failed :) : class Dog < ActiveRecord::Base has_many :features, :through...

How can I just get one record instead of duplicates using ActiveRecord in Rails?

I have a model as follows: Campaign has_many :companies, :through => :contacts There are many contacts with the same company. I just want one instance of each company. I tried the following: @campaign = Campaign.find(params[:id]) @companies = @campaign.companies But this shows me all the companies for every contact I believe. ...

How to declare a specific key for has_many association with :through option?

I have an association: an author has many books; and a book has many authors; I need to use :through option(through a table named 'relations',has two column named 'left_id' (used as author_id) and 'right_id' (used ad book_id); class Relation < ActiveRecord::Base belongs_to :books belongs_to :authors end class Author < ActiveReco...