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...
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...
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...
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...
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...
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...
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...
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...
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, 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...
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)...
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...
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?
...
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_...
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...
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...
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...
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...
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...
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...