has-many

Rails find by *all* associated tags.id in

Hi Say I have a model Taggable has_many tags, how may I find all taggables by their associated tag's taggable_id field? Taggable.find(:all, :joins => :tags, :conditions => {:tags => {:taggable_id => [1,2,3]}}) results in this: SELECT `taggables`.* FROM `taggables` INNER JOIN `tags` ON tags.taggable_id = taggables.id WHERE (`tag`.`ta...

Rails, Deleting Children without Deleting Parent using :has_many

I have a model called MyContainer, which :has_many MyObjects. I want to delete all the MyObjects in the container without having to delete the MyContainer. My model does have :dependent => :destroy, however I don't want to have to delete and re-create the object because it is slower. Something like this does not work: @obj = MyContai...

has_many relation doesn't seems right or logical in business perceptive, needed some thing like belongs_to_many?

My situation is like this. Company has many users and users may belongs to many companies. And current implementation is something like below. class Company has_many :employments has_many :users, :through => :employments end class Employment belongs_to :company belongs_to :user end class User has_m...

How to add a new entry to a multiple has_many association?

I am not sure am I doing these correct. I have 3 models, Account, User, and Event. Account contains a group of Users. Each User have its own username and password for login, but they can access the same Account data under the same Account. Events is create by a User, which other Users in the same Account can also read or edit it. I c...

Grails - Simple hasMany Problem - Using CheckBoxes rather than HTML Select in create.gsp

My problem is this: I want to create a grails domain instance, defining the 'Many' instances of another domain that it has. I have the actual source in a Google Code Project but the following should illustrate the problem. class Person { String name static hasMany[skills:Skill] static constraints = { id (visible:false) s...

How to set up a has-many relationship in Cocoa?

I'm building a (very) simple FTP app in Cocoa, and I need to store information on the different types of servers that are supported. So, I've created a ServerType class, which stores all of the relevant information about a single type of server. I then have a ServerTypes class which is designed to manage all of the ServerType classes tha...

Ruby on Rails has_many minimun

Look: class User < ActiveRecord::Base has_many :scores, :order => 'score DESC',:autosave =>true, end class Score < ActiveRecord::Base belongs_to :user end Now, I want to order the users by the maximun score of each one, The thing is that i need to get a user, two up and two down users like a top 5 scores how can I do tha...

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

RAILS: How to get has_many associations of a model

Hi, how I can get the has_many associations of a model? For example if I have this class: class A < ActiveRecord::Base has_many B has_many C end I would a method like this: A.get_has_many that return [B,C] Is it possible? Thanks! ...

In Ruby on Rails, why will story.votes return an empty Array object, but story.votes.create will actually invoke a method of the Vote class?

In Ruby on Rails, say a Story object can "has_many" Vote objects (a story is voted "hot" by many users). So when we do a s = Story.find(:first) s is a Story object, and say s.votes returns [] and s.votes.class returns Array So clearly, s.votes is an empty Array object. At this time, when s.votes.create is called, it act...

Validations for a has_many/belongs_to relationship

I have a Recipe model which has_many Ingredients (which in turn belongs_to Recipe). I want Ingredient to be existent dependent on Recipe; an Ingredient should never exist without a Recipe. I'm trying to enforce the presence of a valid Recipe ID in the Ingredient. I've been doing this with a validates :recipe, :presence => true (Rails 3)...

How to do STI + self-referential has_many :through

class Addressee < AR::Base end class Employee < Addressee belongs_to :person belongs_to :company end class Person < Addressee has_many :employees has_many :companies, :through => :employees end class Company < Addressee has_many :employees has_many :people, :through => :employees end Given the models above, when I ru...

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

has_many association...class id not found

How can I write the code below so that it passes the user.id. Given what I have, it throws Class id not found error. (User has many fights. And Fight belongs to user. User can either be a challenger in the fight, or a challengee in the other.) has_many :fight_wins, :class_name => 'Fight', :foreign_key => 'challenger_id or challengee_...

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

Create instance of Rails model with has_many association prepopulated

This is best explained by example. The following is simple to do: class Foo < ActiveRecord::Base has_many :bars end 1a>> foo = Foo.new => #<Foo id: nil> 2a>> foo.bars << Bar.new => [#<Bar id: nil, foo_id: nil>] 3a>> foo.bars => [#<Bar id: nil, foo_id: nil>] However, I want all Foo objects initialized with a Bar without having to ex...

Is there a way to break out of the default_scope when using has_many?

I have a tree-like model where in all situations but one, I want to scope the results to only return the roots. class Licence < ActiveRecord::Base default_scope :conditions => { :parent_licence_id, nil } belongs_to :parent_licence, :class_name => 'Licence' has_many :nested_licences, :class_name => 'Licence', :foreign_k...

Rails3: how to set default conditions to has_many

I have boxes and balls. Balls are in boxes. Ball can be either red and green. class Box < ActiveRecord::Base has_many :balls end class Ball < ActiveRecord::Base belongs_to :box scope :green, where(:color => "green") end I want to set has_many only with green balls. I know that finder_sql method exists, but i don't know how to s...

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