belongs-to

How to create "two-side" many-to-many relationships in Rails?

Suppose we have a photography site. Any author can subscribe to receive updates from any other author. Obviously if author A is subscribed to author B that doesn't mean that B is subscribed to A. So we build models class Author < ActiveRecord::Base has_many :subscriptions has_many :subscribed_by_author, :through => :subscriptions, :...

Why am I getting nil for a belongs_to relationship in Rails?

I'm having a problem in my Ruby on Rails app with a model where a belongs_to relationship keeps ending up being nil. Given the following models: class Chassis < ActiveRecord::Base belongs_to :model belongs_to :chassis_size end class Model < ActiveRecord::Base has_many :chassis end class ChassisSize < ActiveRecord::Base...

A field in a rails form to select a 'belongs_to' object

Oh so please bear with me... I have a model for bookings and a model for drivers. A driver has many bookings and a booking belongs to a driver. When adding or editing a booking, I want the user to be able to type the username of the driver. Usernames are unique and easier for the end user than giving each driver a number. Now this is h...

Rails belongs_to association, can't access owner's attributes when part of a collection?

I have an Object, Ball, which belongs_to a Girl, which can have_many balls. Everything works for the most part, but if I try to print out the girls' name via: @balls.each do |b| b.girl.name end I get the following error: "undefined method `name' for nil:NilClass" Which really confuses me. If I say b.girl.class, I get it as an i...

Validate a belongs to association in a build situation.

I have a Mission model that has_many Task, and the Task belongs_to Mission For security I've made this validation on the Task Model: validates_presence_of :mission_id validates_numericality_of :mission_id But the problem is that when create a Mission and add tasks like this: @mission.tasks.build The validation returns error,...

Rails Model Relationship: Has one but also belongs to many

I have two Models, Modela and Modelb. Modela can only own one Modelb, but Modelb can be a part of many Modela's. What I have right now is class Modela < ActiveRecord::Base has_one :modelb end class Modelb < ActiveRecord::Base belongs_to :modela, :foreign_key => "modela_id" #might not make sense? end Not too sure about the whole...

PHP/MySQL: Check if something 'belongs-to' something else?

I have two tables: Stores and Items. The relationship is: Stores 1---* Items In PHP/MySQL what would be the best (fastest/simplest) way to check if a particular item belongs to a particular store. In other words given for example: $store_id = 1; $item_id = 12; I want to check if item 12 belongs to store 1 (and not some other store)...

How do I add and remove multiple "belongs_to" instances to and from a "has_many" instance?

I currently have two models: Campaigns and Videos. Videos belongs to Campaigns and a Campaign has many Videos. In my Campaign form I want to be able to add Videos that have no parent and also be able to remove Videos that belong to the selected campaign. I came up with using two separate multiple selection lists for this. One list has al...

CakePhp: model recursive associations and find

Hello to everybody! I've some trouble with a find() on a model on CakePhp. I have three model relationed in this way: Project(some_fields, item_id) ------belongsTo-----> Item(some_fields, item_id) ------belongsTo-----> User(some_fields campi, nickname) I need to do a find() and retrieve all fields from project, a field from Item and ...

Using accepts_nested_attributes_for with a belongs_to association, and using find_or_create_by_attr behaviour

I am building a Rails application which allows a user to create an object that refers to multiple other models, creating them if they do not exist, and just associating to ones that already exist: # app/models/upload.rb class Upload < AR:B belongs_to :user belongs_to :observed_property belongs_to :sensor accepts_nested_attribut...

Let a question always :include its author, how?

class Question < ActiveRecord::Base belongs_to :author end class Author < ActiveRecord::Base has_many :questions end When I find some questions, I usually need to get their authors at the same time, so I use: Question.find(:all, :include=>:authors) But I don't write the ":include" part everywhere. I hope I can define the "inc...

belongs_to with a custom class_name not producing proper foreign key in Rails 3

I am updating an application to Rails 3 and I am having trouble creating a custom foreign key. I have something like this: class Product < ActiveRecord::Base belongs_to :owner, :class_name => 'User' ... end class User < ActiveRecord::Base has_many :products ... end class ProductsController < ApplicationController before_filter ...

has_many, belongs_to association where has_many associated model has two alias fk in belongs_to associated_model...

I have a User model that has many fights. Fight belongs to User. There are two foreign keys in the fight table that reference back to the user PK -- challenger_id and challengee_id. The trick is how do I write the has_many association on the User model so that it returns fights where user_id = challenger_id or challengee_id? ...

In Rails, how can I retrieve the object on a belongs_to association, without going through the database?

Consider the following setup: class Parent < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base belongs_to :parent end And this console session: >> p = Parent.find 41 >> p.some_attr = 'some_value' >> c = p.children.build >> c.parent By watching my log files, I can see that c.parent is querying the db fo...

RoR: How to print all elements that belongs_to this table

Ok, I'm not sure that my title was clear enough, but I will try to explain I have two tables: orders that has_many items and items that belongs_to orders. I just started to learn RoR and stuck with a simple task. All I want is to display orders and related items like this: Order 1: Item 1 Item 2 Order 2: Item 1 Item 2 ... I k...

Should I use has_one or belongs_to in ruby on rails?

I want to have a model called Status which will be relatively static after some user-defined set up (and different users may have different values on status). The status can apply to different Models, such as Contact and Events. so contact.status set of possible values will be different from event.status I want to design the app so th...

Containable behavior does not return from depth 3 - CakePHP

Hi, I use CakePHP 1.2.6 and have the following relations: Showcase HABTM User belongsTo Galleryitem hasOne Image I try to get all the data related to a Showcase, and therefor also all its users with their Galleryitem -> Image. I use the following query: $showcase = $this->Showcase->find('first', array('conditions'=>array('Showcase...

ActiveRecord relations when model has both one and many of the same model

My data resembles this: class Team < ActiveRecord::Base has_many :persons has_one :leader end class Person < ActiveRecord::Base belongs_to :team end Person only belongs to one Team, but of the many team members there is only 1 leader. First question: should I use belongs_to instead of has_one in the Team model? Second: Team i...

Disordering of a has_many association with belongs_to reference into the has_many

My data resembles this: class Team < ActiveRecord::Base has_many :persons belongs_to :leader, :class_name => "Person" end class Person < ActiveRecord::Base belongs_to :team end I create the Team like this: @team = Team.new for (each new person as p) new_person = @team.persons.build new_person.name = p.name if...

Rails: Non id foreign key lookup ActiveRecord

I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes, :foreign_key => 'user_name' end When I do a coach_obj.coach_classes, this rightly triggers SELE...