associations

Help clarify my thinking for rails associations

I'm building a rails app that allows athletes to create and manage timesheets for the sport of skeleton. I can't seem to wrap my brain around the correct associations between my models. Here's what I have so far. Athletes have Runs, which are composed of several Splits. Each individual split should be associated with a particular timing ...

ActiveRecord gives 'no such column' SQL error for simple has_many association

This is driving me insane! This code used to work fine, but as of a few weeks ago it stopped working, and I can't work out why. Basically a Game has many Patches. The error occurs in my PatchesController, but its reproducible in the rails console like this: first_game = Game.find(:first) first_game.patches As soon as I use the patches...

ActiveRecord find through has_one association child attribute

I have models like this: class Discussion < ActiveRecord::Base has_many :comments has_one :special_comment, :class_name => "Comment" end class Comment < ActiveRecord::Base belongs_to :discussion # contains author end How can I select every Discussion through its adjoined :special_comment 'author' association. Effectively I w...

Ruby on Rails - Overriding the association id creation process

Hello there, I'm trying to override the way rails apply and id to an associated object, for example: There are 2 simple models: class Album < ActiveRecord::Base has_many :photos end class Photo < ActiveRecord::Base belongs_to :album end And then I want to do this: album = Album.new :title => 'First Album' album.photos.build alb...

Hibernate: Shallow copying of ManyToMany associations without initialization of target entities

There are two entities: @Entity class A { @ManyToMany List<B> bs; } @Entity class B {} I now want to duplicate (clone) an instance of class A. Is there any way with Hibernate to make a shallow copy of the persistent collection bs without having to completely initialize all Bs? In SQL it would be quite easy because you would just ...

Complex ActiveRecord association definition

How would you map the dashed association using ActiveRecord? Is there any ActiveRecord-way of doing it? What I want is to get all unique available promotions for a particular item. Here are things I thought but I don't really like: Using custom finder class Item < ActiveRecord::Base has_many :promotions, :finder_sql => "SELECT..FR...

Rails join tables or HABTM associations

In the application I'm building, I want to have Events. People can then register for the event by creating a team, by directly registering for an event (no team), or by joining an existing team. I originally set it up as Event has_many teams, Team has_many users, user belongs_to Team and Event. However, I foresee problems if a registe...

How to select only one element of associated objects in to_json / to_xml

I have the following problem. My App (a prototype of an api) should be able to react on params (like language). I'm already able to respond to an request by sending an entierly object (with associated objects included) as JSON. But now i want to select the associated objects by parameter. my code: object.to_json(include => { :texts_...

mongoid references_many referenced_in not working rails

Hello frens, I have a problem with referenced_many and referenced_in relation model. My model is of following. Student references_many mobile_numbers MobileNumber referenced_in Student Now when i try to do @mobile = MobileNumber.first @mobile.student It pops error saying Document not found for class Student with id(s) 4c47e74ff1936f05f...

Joins Tables & Rails

I have events and users/teams. class Event has_many :users, :through => :registrations end class User has_many :events, :through => :registrations end class Registration belongs_to :users belongs_to :events end When I register a user, I'm connecting them to the event like so: @event.users << @user Does this implicitly creat...

how to build on ActiveRecord association before parent object has an id?

Hi, I have a model, Foo, that has_many Bars. It has a virtual attribute, current_baz, that returns the baz attribute of the latest Bar from the association (if one exists). I'd like to define Foo.current_baz= so that one can say my_foo.update_attributes(:current_baz => 7), and in particular, I'd like it to work when a new Foo is created...

EF4 prevent child entity attached

We have a situation with a fairly complex entity/table hierarchy modelled with EF4. Often we like most of the associations to load (whether lazy or not) and are happy with that. Sometimes we want just the top of the hierarchy and maybe one or two of the related detail tables to be attached. For example when returning a set from a WCF ...

creating models with multiple references in rails

I have a model defined as class A < ActiveRecord::Base belongs_to :b belongs_to :c end How do I create a new instance of A associated with both b and c. I've got the ids for b and c. ...

Tricky Association Question Rails

#Vote Model belongs_to :voteable, :polymorphic => true belongs_to :voter, :polymorphic => true #votes table Voteid: integer, vote: boolean, voteable_id: integer, voteable_type: string, voter_id: integer, voter_type: string, created_at: datetime, updated_at: datetime #User has_many :voteables, :foreign_key => :voter_id, :class_name =...

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

Add further information to Rails Select list?

Hi Everyone, I have a select list drop down in my Rails application, in the form for creating a new record. <li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li> This is so the user can choose from a list of Surveyor's ...

belongs_to association fails to load target parent referred from a setter method on mass assignment

It seems that when a child object has a reference to its parent in its setter, it fails to get initialized unless the foreign key is given first in the parameter hash. class Bot < ActiveRecord::Base has_many :items end class Item < ActiveRecord::Base belongs_to :bot def name=(text) write_attribute(:name, "#{self.bot.name}'s ...

Rails: Conditionally loading associations

Hey! I have models club and course where a course belongs to a club and and a club has many courses. When I load a club, I also want to load it's associated courses, but I only want to load those that meet a conditional test (approved? == true). It is straightforward how to do this if I were working directly with the courses: @course...

two association belongs_to with the same models

I have two classes : User, Patent and Help (belongs_to :user and belongs_to :patent) When I click to a link I must create an "Help" that refer to the patent and also to the users. One user is the helper and the other one is the caller. patents_controller: def create @patent = Patent.find(params[:patent_id]) @patent.help...

CakePHP associations messing up

Hi, I am new to cakephp and have been working through the Apress book "Beginning CakePHP from Novice to Professional" but have ran into a problem after the baking chapter. (If you've seen my other recent question, you'll know that I had to skip that chapter because I cant get the bake console working on my win7 machine). The problem i...