Questions tagged [associations]

Associations typically refer to relationships between models in ORMs such as ActiveRecord.

Associations typically refer to relationships between models in ORMs such as ActiveRecord.

Typically associations are classified based on the number of associated models:

  • one to many: one of the models stores the ID of the other
  • one to one: models (which 'belong to') each store the ID of the one associated model (which 'has many')
  • many to many: the models are linked via a join table in the database which may (called sometimes 'has many through') or may not (called 'has and belongs to many') be a model itself
6131 questions
1
vote
1 answer

has_many extensions, are dynamic conditions cached? Does one need a lambda expression to suppress caching?

I am maintaining a Rails 2.3.x app. Part of it deals with Users and Roles. A user can have a number of roles, each either for some period or indefinitely. I'm trying to write a has_many extension to quickly find all roles that a user currently have…
Swartz
  • 1,051
  • 2
  • 11
  • 23
1
vote
1 answer

Ruby on rails - what should a controller look like when dealing with collections?

I have a simple application in which the user can manage decks of cards. In my model I have: Card DeckOfCards In my view /DeckOfCards/:id/edit I want to allow the user to create a new card and add it to the current deck. I currently have a…
Chris
  • 6,076
  • 11
  • 48
  • 62
1
vote
2 answers

If X has_one Y, do you have to also say Y belong_to X?

I am currently reading A Guide to Active Record Associations. I get that you choose between has_one or belong_to depending on how you'd like to lay out the association. But I am not entirely clear if there is difference between the…
Jason Kim
  • 18,102
  • 13
  • 66
  • 105
1
vote
3 answers

How to set up associations in Ruby on Rails?

I'm building a sample app for practice and am having trouble determining the best way to organize my models and associations. So let's just say I have 3 models: Schools Classes Students I want: schools to have many classes classes to have many…
Zach
  • 1,233
  • 13
  • 19
1
vote
1 answer

Associations in CakePHP with non-conventional database

We are converting an application for use with CakePHP 2.0.3. For some reason, I cannot seem to set proper relations between my models. Here's an example: User (id, petid, country, picid, ...) Pet (id, userid, picid, ...) Picture (id, albumid,…
Kukiwon
  • 1,222
  • 12
  • 20
1
vote
1 answer

Does type of relationship matter with Rails associations?

When using has_many :through => :something, does the type of relationships matter? E.g., If A has_many C :through => :b, does it make a difference whether model A belongs_to B which has_many C or model A has_many B which has_many C?
John
  • 13,125
  • 14
  • 52
  • 73
1
vote
1 answer

Rails 3 find model attribute with foreign key in another model

I have two models, Reports(belongs to client) and Clients(has many reports). A Client has an attribute or column called "specialty". What I'm trying to do is to be able to call and display the Client.specialty attribute for that particular @client…
FattRyan
  • 906
  • 2
  • 12
  • 26
1
vote
1 answer

Querying through associations with Ruby on Rails

I have a question about doing a query through a few associations using Ruby on Rails. This question involves querying across three models. (Event, Fight, Fighters). The important parts of the models are as follows: An event has multiple…
1
vote
1 answer

Extending windows explorer context menu

I'm having hard times figuring out why this doesn't work on my computer. I've read this article http://msdn.microsoft.com/en-us/library/bb776820.aspx and tried it, and it works for an unknown file type, but for know such as .bmp it doesn't - I've…
n1tr0
  • 269
  • 4
  • 15
1
vote
2 answers

python read data build association rules

I have two columns in a text file. I read them into Python into two separate lists. What I want to do is count the occurences of each pair and build association rules based on it. Example: colA = [a,b,c,d,...] colB = [c,y,d,e,...] I came only so…
user366121
  • 3,203
  • 9
  • 48
  • 69
1
vote
1 answer

How to access the original model inside of an ActiveRecord association method

I've been using ActiveRecord association methods (such as in this post Ryan's Scraps) to perform some of my more complex 2nd level associations, while minimizing query counts. For example, I use the following association to pull all activity from…
Justin
  • 1,203
  • 8
  • 15
1
vote
2 answers

Rails, has_many, belongs_to

Schema: create_table "reports", :force => true do |t| t.integer "user_id" t.string "apparatus" t.string "capt" t.text "body" t.datetime "created_at" t.datetime "updated_at" end create_table "users", :force =>…
bennett_an
  • 1,718
  • 1
  • 16
  • 35
1
vote
0 answers

Validations outside the scope of a record

I have a report model which has many tools. Each tool has a working_hours attribute. The working hours attribute represents the total hours the tool has been used - you know, to keep track of servicing times. The idea of the validation in question…
Igbanam
  • 5,904
  • 5
  • 44
  • 68
1
vote
0 answers

Are backrefs necessary when using association proxies for a many-to-many?

So using SqlAlchemy, I'm creating a fairly simple many-to-many relationship between a users model and a comments model. users.py class UsersModel(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) username =…
JayD3e
  • 2,147
  • 3
  • 21
  • 30
1
vote
2 answers

grails / gorm supporting different parent entity in 1 - many associations

I have the following "top-level" (parent) domain entities: Customer Company Contact And the following child entity: Address There is one to many relationship between each of the top level domain entities: Customer -> Address Company ->…
Conor Power
  • 686
  • 1
  • 6
  • 17
1 2 3
99
100