Suppose you have two models, User and City, joined by a third model CityPermission:
class CityPermission < ActiveRecord::Base
belongs_to :city
belongs_to :user
end
class City < ActiveRecord::Base
has_many :city_permissions
has_many :users, :through => :city_permissions
end
class User < ActiveRecord::Base
has_many :city_permi...
Hey guys!
Sorry, if this is a noobish question, but I'm just getting started with Rails and jQuery. I have the following scenario:
I have 3 classes: contacts, companies and contact_company_joins (ccj).
For all three classes I created models, controller and views. Obviously, contacts and
companies are connected via the join-table ccj (...
Suppose the following data schema:
Usage
======
client_id
resource
type
amount
Billing
======
client_id
usage_resource
usage_type
rate
In this example, suppose I have multiple resources, each of which can be used in many ways. For example, one resource is a widget. Widgets can be fooed and they can be bared. Gizmos can also be foo...
I've got three (relevant) models, specified like this:
class User < ActiveRecord::Base
has_many :posts
has_many :comments
has_many :comments_received, :through => :posts, :source => :comments
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
be...
Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, but obviously I can't repeat the :children in a has_many (the first gets overwritten).
class Person < ActiveRecord::Base
belongs_to :fath...
Hey guys,
how can I achieve the following? I have two models (blogs and readers) and a JOIN table that will allow me to have an N:M relationship between them:
class Blog < ActiveRecord::Base
has_many :blogs_readers, :dependent => :destroy
has_many :readers, :through => :blogs_readers
end
class Reader < ActiveRecord::Base
has_many :...
I have two models (users and courses) and a JOIN table that allows enrollments in the course:
class User < ActiveRecord::Base
has_many :enrollments, :dependent => :destroy
has_many :courses, :through => :enrollments
end
class Course < ActiveRecord::Base
has_many :enrollments, :dependent => :destroy
has_many :users, :throu...
In my application, a user has_many tickets. Unfortunately, the tickets table does not have a user_id: it has a user_login (it is a legacy database). I am going to change that someday, but for now this change would have too many implications.
So how can I build a "user has_many :tickets" association through the login column?
I tried t...
New to both Ruby and Rails but I'm book educated by now (which apparently means nothing, haha).
I've got two models, Event and User joined through a table EventUser
class User < ActiveRecord::Base
has_many :event_users
has_many :events, :through => :event_users
end
class EventUser < ActiveRecord::Base
belongs_to :event
belongs...
I'm wondering what the easiest/most elegant way of selecting attributes from join models in has_many :through associations is.
Lets say we have Items, Catalogs, and CatalogItems with the following Item class:
class Item < ActiveRecord::Base
has_many :catalog_items
has_many :catalogs, :through => :catalog_items
end
Additio...
In a rails app, I using the :finder_sql option in a has_many declaration. The rails docs say that when I do this, "find_in_collection is not added." What does this mean?
...
Hello there,
I am having trouble getting this to work.
My problem is. I have A number of models (Article, Video, Photo)
Now I am trying to create a related_to association, such that
An article can have many other articles, videos and photos related to it. As can videos and photos.
Heres what I have tried
module ActsAsRelatable
def...
The following db design example is given in the Agile Rails book to teach the has_many :through code...
[Article]----<[Readings]>----[User]
This is all quite easy to understand at first. However, I have a situation where the following db design might be used:
[Genre]-----<[Article]-----<[Readings]>-----[User]
Now here is my questi...
Let's say I have models: User and Item and relation many-to-many between them. How to get Users who have exactly(no more) Items with the defined attributes i.e. Users who have Items with colors = ['red', 'black'].
Of course I can do something like this:
User.all :joins => [:items, :items], :conditions => {:"items.color" => "red", :"...
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...
Hi,
I have a relationship table :
create_table "animal_friends", :force => true do |t|
t.integer "animal_id"
t.integer "animal_friend_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "status_id", :default => 1
end
linking animals to others. Best way to retreive associations in SQL is :
SE...
What is the "rails way" to efficiently grab all rows of a parent table along with a count of the number of children each row has?
I don't want to use counter_cache as I want to run these counts based on some time conditions.
The cliche blog example:
Table of articles. Each article has 0 or more comments.
I want to be able to pull how ...
Howdy,
I'm a Grails noob and running into something that seems to be a bug, but it is entirely possible I'm not configuring everything correctly.
I've got two simple Domain Classes:
class Player {
String firstName
String lastName
static constraints = {
firstName(blank:false)
lastNam...
Hello, I have a has_many relationship between two entities, Feeds and Posts. I also have specific types of posts, Videos and Photos. This is structured in the database using single table inheritance.
Right now I have my Feed model specifying a has_many relationship between Feeds and Posts (including the subtypes)
class Feed < ActiveRec...
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...