belongs-to

Can you force activerecord associations to update without saving them?

I have a couple models which are associated with the has_many belongs_to pair. For the sake of demonstration, a client has one server but a server has many clients. I might do something like this: client1.server = the_server client2.server = the_server client3.server = the_server My actual application is quite a bit more complex tha...

Rails: belongs_to vs has_one

A bit of a newbie question on rails associations. I have a Bug model, and a Status model. Status is basically just a key/value pair table. Out of the choices available, I would say Bug has_one Status makes the most sense. However, according to this Content belongs_to ContentTemplate. Go back and look at how I described the probl...

Using ActiveRecord belongs_to with two keys

Hi, I have two ActiveRecord models with a hasMany / belongsTo association: class User < ActiveRecord::Base has_many :letters end class Letter < ActiveRecord::Base belongs_to :user end The User model has a revision_number attribute, to which I would like to scope the belongs_to association, so the letter is associated to a User b...

BelongsTo problem in cakephp and html select, i can't understand how to do that

Simple question by a cakephp noob: i have two models, Player and Team. Team has an id (int) and a cool_name (varchar). Player has an id (int), a cool_name (varchar) and a reference for the team table, team_id (int). Cool_name instead of name because i don't have tables in english, so i can't use the field 'name'. So, a team hasMany ...

Has_Many, Belongs_To Relationship in a Database Driven Application

I am working on an an application that allows a user to search for a particular book on Amazon using a given search criterion (e.g. title, keyword, author.) I am currently attempting to store these search results to a MySQL database, but am struggling with the concept of relationships. It seems natural that I would structure the models ...

Ruby on Rails prevent nil error when it is assumed record may not exist

I am building a simple book check out application. One of the things that I need to do is determine if a book is checked out. I have my associations between my people and book classes setup through a book_check_out class. My goal is to use the checked_out property of book to determine if a book is presently checked out. However, in my pr...

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

Zend Framework isValid problem with "belongsTo" option on elements.

I am building an application with a fairly hefty form that is built using 11 sub-forms (and I will mention that they are all ZendX_jQuery forms). In one of the sub-forms I have a few quirkier elements. I need a list of features about rooms in a house. So I require multiple text input boxes that belong to an array - these inputs are the...

Ruby on Rails: Nested Attributes, belongs_to relation

Hello guys, I hope you can help me. I have a User entity that has a Current Location field (city and country). To old this info I created an entity called Location which has_many Users. I'm not entirely sure if I should put in the User model "has_one" or "belongs_to", but for what I read if I wanted it to have the foreign key of the lo...

Pagination with hasMany association cakePHP

I have two tables: Contestant and Votes Contestant hasMany Votes I've tried doing a count(Vote.id) as Votes so I can place it on the recordset and just paginate them but I have no idea where to place it. I did it on the fields array but it gave me the total count of votes regardless of the contestant they belong to. The votes are link...

Implementing 'belongs' with LINQ queries

I have a List with the numbers (5,9,3) in it. Let's call it MyList I would like to perform var results = from a in myEntities.thing1 where a.ID belongsto MyList select a; right now I do List<T> t = new List<T>(); //I actually define T to a strong type foreach (int i in MyList) { t.add(from a in myEntities.thing1 where a.ID==i sele...

Rails: save a record via belongs_to and setting the foreign key

Is there a better way of writing this code? It just doesn't sit right with me, I feel like there is something really 'rails like' that I should already know: belongs_to :parent_in_use belongs_to :parent_template def after_create new_parent_in_use = ParentInUse.create!(self.parent_template.attributes) self.update_attribute(:parent_i...

Relation between two single-table inheritance models

Hi, I have the following two models class ContactField < ActiveRecord::Base end class Address < ContactField end class Phone < ContactField end and class Contact < ActiveRecord::Base end class Company < Contact end class Person < Contact end I want one contact, no matter is it Company or Person, to have many ContactFields(Addre...

ActiveRecord has_one relationship does not return in certain cases

Given three models that are each nested in each other. If I create the top-level object and build_* the other child objects, I can retrieve all child objects through the relationships before and after save() on the original instance. However, if I try to retrieve the 2nd level nested object after find(:id) the original parent it fails. I...

Optional belongsTo relationship in cakephp

Hi SO, I have a simple Category model in my CakePHP application. I want to add sub-categories, and do this by simply adding a parent_id column. The parent_id is a belongsTo relationship, that references back to the same Category model. When I generate my admin scaffolds, a dropdown will shop up (correct!), but I'd like to include a NU...

Zend Framework problems using Element, sub-forms and belongsTo

Still pulling my hair out with Zend_Form and any elements that need to be placed in a sub-array. form->populate() does not work when working with elements within a sub-form that have been set to a parent array using belongsTo() - I think it is actually a bug in Zend_Form->setDefaults() but just wanted to see if anyone else has a) had the...

Setting up a mutual belongs_to in Ruby on Rails

I'm creating a wiki. Each Article has_many Revisions, and an Article belongs_to a single current_revision. So in the database, Articles have a single reference to a Revision's id, and Revisions each have a single reference to the Article they belong to. Before I continue, does this seem like a sane way to do things? It strikes me as ...

ActiveRecord :include not working for belongs_to with foreign_key

I have a two models set up like this: class User < ActiveRecord::Base # external_id column in database end class UserUpload < ActiveRecord::Base belongs_to :user, :primary_key => "external_id", :foreign_key => "external_user_id" end However, whenever I do upload = UserUpload.find(id, :include => :user) The sql that gets emitte...

Rails: With models A has_many B belongs_to C return A where B belongs to C and B is most recently created for given A?

I have three models A, B, and C, where A has_many B; B belongs to C, and B has a created_on timestamp. C has_many A through B. I want to return all A where A has a B that belongs to C and where B is the most recently created B for that A. I can do this with a method loop. Can this be done solely with named_scopes?, or is some other ...

Rails ActiveRecord associations inconsistently updated

I am running into some Rails 2.3.5 ActiveRecord behavior I do not understand. It appears that an object can have its association ids updated in inconsistent ways. This is best explained with an example: Create a Post model with the string attribute 'title' and a Comment model with the string attribute 'content'. Here are the associa...