associations

How can I use a different id field for one of my CakePHP model associations?

I have an one to many association in which a Thing can have many Statuses defined as below: Status Model: class Status extends AppModel { var $name = 'Status'; var $belongsTo = array( 'Thing' => array( 'className' => 'Thing', 'foreignKey' => 'thing_id', ); } Thing Model: class Thing exten...

Ruby on Rails associations for set theory (union, difference, intersection, etc.)

I would like a has_many association that works like so: class Hood acts_as_tree has_many :houses, :union_with => :parent end class House end where any House associated with Hood 1 would also be returned in .houses of subhoods of Hood 1, along with the subhoods' individual associations. The association only needs to work from the...

RoR associations through or not through?

I have four models that are related to one another, the way I have it setup at the moment is I have to select a county, region and country when entering a new city. class Country < ActiveRecord::Base has_many :regions has_many :counties has_many :cities end class Region < ActiveRecord::Base has_one :country ha...

Association and model data saving problem

Developing with cakephp 1.3 (latest from github). There are 2 models bind with hasAndBelongsToMany: documents and tags. Document can have many tags in other words. I've add a new document submitting form there user can enter a list of tags separated with commas (new tag will be added, if not exist already). I looked at cakephp bakery 2....

how do I associate one model twice to another

Hi im making a small site to help me and friends learn languages. Typical use: Adam is english but is learning japanese. Adam can practice his japanese by writing and submitting articles written in japanese. Adam cant (not allowed to) submit any articles written in his native language. Adam can read articles (written in English) by ot...

Relationship among models[Rails]

Hi I am Having 3 Models Category / Issue / Article Where Article is taggable_on :topics and :sub_topics Using [acts_as_taggable_on plugin] So what kind of relationship we have to maintain among these model and how we can retrieve category topics,sub_topics / issues topics and sub_topics Thanks in advance ...

How to use variable named scopes with attributes of associated models OTHER than id

Hi, I'm having trouble with getting a named scope working using using an attribute of the associated model other than the id column. I have a Firm model which has a city_id column. I also have a City model with a name column. I want to get restful urls like this so as to make use of the has_scope gem and have skinny controllers http:...

JPA query many to one association

I want to build the following pseudo query Select a From APDU a where a.group.id= :id group is a field in APDU class of the type APDUGroup.class. I just want to get a list of APDUs based on APDUGroup's id. How do i do that using a standard JPA query? UPDATE Yes, I have tried the above query and tried other variations for hours befo...

Rails - Associations - Automatically setting an association_id for a model which has 2 belongs_to

I have 3 models class User < ... belongs_to :language has_many :posts end class Post < ... belongs_to :user belongs_to :language end class Language < ... has_many :users has_many :posts end Im going to be creating lots of posts via users and at the same time I have to also specify the language the post was written in, wh...

Rails User Rating Another User

Hello, I am working on an application where a user has the ability to leave feedback on another user. Currently, I already have an implemented user model. Is this considered a self referential association? This seems a bit wierd to me (how to set it up through Active Record Associations). How would I go about setting this associatio...

rails Creating a model instance automatically when another is created

Hello I have a user model and a ratings model. Whenever a new user is created I want to create a new feedback model with it automatically. Each user model has one feedback model and each feedback model has many ratings. My Classes class User < ActiveRecord::Base end class Feedback < ActiveRecord::Base belongs_to :user has_many ...

Model associations

I have two models Library and Book. In my Library model, I have an array - book_ids. The primary key of Book model is ID. How do I create a has_many :books relation in my library model? This is a legacy database we are using with rails. Thanks. ...

Including associations optimization in Rails

Hey, I'm looking for help with Ruby optimization regarding loading of associations on demand. This is simplified example. I have 3 models: Post, Comment, User. References are: Post has many comments and Comment has reference to User (:author). Now when I go to the post page, I expect to see post body + all comments (and their respectiv...

How can a record belong_to a single user, but also have multiple "secondary" users?

In my app, I have a User model and a Project model. A user has_many assignments and each project belongs_to a user. But along with each project having an owner, the user who created it, I would like the owner be able to share it with others (so that the project gets shown on the other users' account along with their own). I imagine havin...

Refer to similar associated models with a common name

I have these models: class Bill < ActiveRecord::Base has_many :calls has_many :text_messages end class Call < ActiveRecord::Base belongs_to :bill end class TextMessage < ActiveRecord::Base belongs_to :bill end Now, in my domain calls and text messages are both "the same kind of thing" -- i.e., they're both "bill items". So I...

AssociatedControlId of inner namingcontainer

Hi, I have a custom control contains a label control. I want to set the AssociatedControlId of this label to be other control id on the page, but as soon as I implement the INamingContainer in my custom control, it will run into an error saying "Unable to find control with id 'abc' that is associated with the Label 'xyz'." This would be...

Rails HABTM Scoping to current subdomain

I have the following associations: class User < ActiveRecord::Base has_and_belongs_to_many :brands, :join_table => 'brands_users' has_and_belongs_to_many :companies, :join_table => 'companies_users' end class Brand < ActiveRecord::Base belongs_to :company has_and_belongs_to_many :users, :join_table => 'brands_u...

[Ruby On Rails] belongs_to with :class_name option fails.

I have no idea what went wrong but I can't get belongs_to work with :class_name option. Could somebody enlighten me. Thanks a lot! Here is a snip from my code. class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.text :name end end def self.down drop_table...

Complicted ActiveRecord Association. Going through a 4th table

I have kind of a complicated case and am wondering how this would work in rails: I want to categories the genres of some singers. Singers can belong to more than one genres, and users can assign tags to each genre For example: singers <-- singers_genres --> genres <-- genres_tags --> tags SQL would look something like: SELECT * FR...

Best way to handle multiple tables to replace one big table in Rails? (e.g. 'todo_items1', 'todo_items2', etc., instead of just 'todo_items')?

Update: Originally, this post was using Books as the example entity, with Books1, Books2, etc. being the separated table. I think this was a bit confusing, so I've changed the example entity to be "private todo_items created by a particular user." This kind of makes Horace and Ryan's original comments seem a bit ...