associations

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

validates_associated model with condition

hello frens. I have the following validates_associated scenario class Parent include Mongoid::Document validates_associated :son validates_associated :daughter end when i create a parent, either of son or daughter is only created not both. Now my problem is, when i try to create parent with son, then validation fails due to daug...

Object refuses to save changes in Rails IRB

Good morning all, I've got an association that I'm working on, and for some reason, I can't get it to save changes to the object in question when I'm debugging it in IRB. I was wondering if anyone could point out my problem. Here is the association: class User < ActiveRecord::Base has_and_belongs_to_many :affiliates has_one :mana...

CakePhp: On the fly associations using a re-named Model field?

Hi there, I'm trying to use on the fly associations to trim down the data I retrieve, but the model I'm using is associated to other models with a re-named field because I have 2 of the same models associated with it. So, here's the model, say 'test', that has two 'user' fields, both related to the User model. In the model: var $be...

unconventional foreign keys in rails

I have a kind of legacy db structure with rails. It has a structure like: apples id: number: oranges apple_id: (links to apples.number) pears apple_id: (links to apples.id) Models like: Apple has_many :oranges, :foreign_key => ?, :primary_key => ? has_many :pears Orange belongs_to :apple, :foreign_key =>...

Rails 3 model associations. Limit the number of data selected

I have a model user, a feed, and a comment. user has many feeds and comments, feeds belong to user and have many comments and finally comments belong to user and feed. No when i show feeds for a certain user, it selects all the comments made till date for every feed. I want it to select only those comments that were made in the last 1 m...

How to change the sequence of 'joins' in CakePHP?

I have the problem with the sequence of joins. The similar problem was in another question http://stackoverflow.com/questions/3343348/manipulating-order-of-joins-in-cakephp. The answer was to use Containable behavior. In my case that is unacceptable because I have deeper associations and containable generates too many queries. Containabl...

values are lot loaded from the form during validation

I have ruby user model which has its validations written in separate file called validations.rb. This user model has an associated tasks model which has separate validations for it. During validation of user.rb, the code in validations.rb is trying to get the tasks that were entered in the form but it fails and instead gets the existing ...

Need some help with the conditions in CakePHP!

I have three models linked in this manner: Item->Order->Payment Order hasMany Item Order hasOne Payment Now, I am paginating Items and want to add a condition in it to find only items of that order which has payment of a particular id. I hope that makes sense :P I added the condition as: array('Payment.id'=>$id) but it doesn't w...

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

Rails associations.

I am trying to make a private messaging system, where you send a message to one or more users. Now the messages schema is like user_id, to_id, msg How do i make the associations so that i can get the details of the sender and the recipient? ...

Reverse Polymorphic Associations

I have a parent object, Post, which has the following children. has_one :link has_one :picture has_one :code These children are mutually exclusive. Is there a way to use polymorphic associations in reverse so that I don't have to have link_id, picture_id, and code_id fields in my Post table? ...

Why does ActiveRecord only sometimes provide reciprocal associations automatically?

When working with activerecord I notice that while constructing associations, the reciprocal association is provided automatically only if the reciprocal model has already been saved. If it has not yet been saved, this reciprocal association must be defined manually. I am wondering if this is intentional or a bug, since the only differ...

remove_column not removing column or giving any errors in rails

I have a sqlite3 db in a rails app with the following schema ActiveRecord::Schema.define(:version => 20100816231714) do create_table "comments", :force => true do |t| t.string "commenter" t.text "body" t.integer "post_id" t.datetime "created_at" t.datetime "updated_at" end create_table "posts", :force ...

How do I model this relationship in Rails

I'm a bit new to rails so if this is very basic please bear with me. I am creating something like a chat application. My model looks like this (from schema.rb) create_table "people", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" t.integer "session_id" end create_table "session...

Creating a nested JSON request with Rails

I have a bit of a complex situation. I am building an iPhone app with a rails backend. There are two model objects, a chat session and a person. They are defined as follows: class Person < ActiveRecord::Base belongs_to :chatsession end class Chatsession < ActiveRecord::Base has_many :people belongs_to :leader, :class_name => "Person" ...

Is this the correct way to use way :dependent => :destroy in my RoR app?

This is how my RoR app is setup note.rb belongs_to :user has_many :note_categories has_many :categories, :through => :note_categories category.rb has_many :note_categories has_many :notes, :through => :note_categories I want to make it so that when a user deletes a note, the corresponding entry in the note_categories table is del...

Retrieving unique associated models from an array of another model

What is the recommended approach for finding multiple, unique associated models for a subset of another model? As an example, for a subset of users, determine unique artist models they have favorited. One approach is to grab the users from the database, then iterate them all quering for favorites and building a unique array, but this se...

before_destroy is not firing from update_attributes

I have a student that has many courses. In the student#update action and form, I accept a list of course_ids. When that list changes, I'd like to call a certain function. The code I have does get called if the update_attributes creates a course_student, but does not get called if the update_attributes destroys a course_student. Can I get...

Searchlogic on associations

I have the following associations: class BookShelf { has_many :books } class Book { has_many :pages belongs_to :book_shelf } class Page { belongs_to :book } If I have a BookShelf object, how could I use searchlogic to search pages that belong to the books in the current book shelf? Currently I'm using this one: @bookshelf ...