associations

Cakephp model associations and amount of mysql queries they require

I have a rather simple data model, but I have a centric entity called "item" which is associated to 7 other entities via 1-to-1 / 1-to-many relationships. Everything works smooth. But in most actions that work with the item model, I dont need any of the associations from the item Model - so mysql ends up issuing a many unnecessary querie...

In Rails, how can I retrieve the object on a belongs_to association, without going through the database?

Consider the following setup: class Parent < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base belongs_to :parent end And this console session: >> p = Parent.find 41 >> p.some_attr = 'some_value' >> c = p.children.build >> c.parent By watching my log files, I can see that c.parent is querying the db fo...

Ruby on Rails Thinking Sphinx facets

I have an application in which I need to implement the faceted search functionality on one of the fields of the associated models and it does not seem to be working. Here is a brief context: There are 3 models I am working on: 1. Product 2. Attributes 3. ProductAttributes. Please see the code spnippets below: class Product < ActiveReco...

interesting rails association challenge

I have stumbled on an interesting challenge regarding active record associations: I have an Account model which can have multiple and different organisations attached to it (like for example a Company, a Contractor, a Person) and it also has a different role with each association (accountant, owner, viewer, etc.). So I am not sure what...

MongoDB and CakePHP Model associations

Hi, I'm trying to make a Match model which includes the players lined up during that match. So each Match hasMany Players and each Player hasmany Matches. match = { '_id' : ObjectID('978tqwbi9873gofiu'), 'home' : 'Argentina', 'away' : 'Brazil', 'lineup-home' : [ {'name' : 'Lionel Messi', 'goals' : '2', 'timeon' : 30 }...

when do we draw association?

Class Engine has "start(c:Component)" method. So do we need to draw an association between Engine and Component Class IF there is no "new Component()" inside Engine class. Thanks & regards ...

Extend NSMutableArray for bi-directional association management

I am a great fan of code generation (from UML) and coming from the Java world, I wonder how I would implement automated bi-directional association management in Objective-C. Image an association Partner <-> Address, one-to-many and navigable from both ends. What I would like to achieve is that if I append an Address to a Partner that Ad...

Versioned associations using vestal_versions?

I'd like to be sure if vestal_versions does support versioned associations (it seems like it doesn't) before switching out to another versioning gem that can support versioned associations e.g => has_versioning. I haven't looked at the code yet but I couldn't find anything related with versioned associations from the readme file or the i...

Rails Polymorphic has_many

Using Ruby on Rails, how can I achieve a polymorphic has_many relationship where the owner is always of a known but the items in the association will be of some polymorphic (but homogenous) type, specified by a column in the owner? For example, suppose the Producer class has_many products but producer instances might actually have many ...

ruby on rails association

how to create dynamic association in rails ...

Is it OK to use "semantically reversed" associations, like Message blongsTo Attachment, in CakePHP?

Suppose that we have the following partial ER diagram: Notice that the attachments table will subsequently be used for the messages', submissions', assignments', and lectures' attachments. The problem is with the 3 one-to-one relationships between attachments and messages, submissions, and assignments. According to CakePHP's conventi...

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

CakePHP 1.3 associations

I've started writing a project using CakePHP (1.3) and am finding it difficult to return results using the CakePHP style of querying. The project is an issue tracker. So far I only have the following tables - 'users', 'projects' and a link table 'projects_users' because any users can be associated with one or more projects. There is als...

two resources with two different relationships in rails

I am new to rails, and have a situation that I can't quite get my head around. Lets say I have two resources, users and widgets. Users can use widgets, but widgets are also user created, and should be owned by the user that created them. There needs to be a user uses widget, and a user owns widget. Is the following what I am looking ...

Rails Paperclip with model association

I have a page where I can view a product, and directly from this page the user can add and remove multiple images associated with the product. I'm using paperclip in a form to upload new file. Because multiple images can be saved for a product I created an image model belonging to the product model. It's not possible to use the default...

DataMapper association: How do I specify table name that contains the associated rows?

I am working with a database that is already in place and used by other applications. The people who designed the database did not use pluralized table names, so DataMapper chooses the wrong table name when following associations. For instance: class Foo has n :components # => the table name here should be COMPONENT, but datamapper ...

Adding a Rating to each User per category on Ruby on Rails

Right now I'm building a social media app, where i want an user to have a rating per category, how would the association go? The way it needs to be setup it's Each user will have a different rating in each category. I'm think that belongs_to :user belongs_to :category in the UserCategoryRating model. and has_many :user_category_r...

Rails ActiveRecord associations

I have an User model. How I can get all associations between the User model with other models ? I need to know the model names and the association type (1..1, 1..m, m..m ...) I don't want to use the db/schema.rb file ...

Has_and_belongs_to_many accepts_nested_attributes_for problem

I am working on some code today but for some reason I can't seem to get it to work. Some explanation: I have a system that contains feeds, that consist of feed_entries. There are also some categories with sub_categories. Between the sub_categories and feed_entries there is a has_and_belongs_to_many relationship, which I can not seem to ...

ActiveRecord and has_many, :through link backs

Is there a simple way to specify a has_many :through relationship where you're linking the same data type? eg. One User has many friends (who are all users). I'd like to be able to store data about the friendship, so has_many :through seems to be the obvious choice, but then you'd have to define two :user_id columns, which of course does...