associations

Associations with Doctrine PHP

Hello, I'm trying to do an association of two objects with Doctrine (PHP). I have two objects : User and Conversation One user has many conversations and a conversation belongs to two users maximum (on initiator of the conversation, and one receiver). So, in my Doctrine class I have this in the Conversation class : $this->hasOne('Us...

How can I optimize this situation with ActiveRecord (Rails 3)

Let's say I have the following association: Club has_many users User has_many guns Gun has_many bullets Club: Moe, Larry, Curly Moe: 2 guns gun 1 has 100 bullets gun 2 has 20 bullets Larry: 1 gun gun 1 has 40 bullets Curly: 2 guns gun 1 has 20 bullets gun 2 has 10 bullets Now, I want to find out how many bullets in the CLUB. It'...

How to create related records with Doctrine PHP

Hello I have modeled two classes with a many to many relationship : User and Conversation, and I can't create a the link between these two classes when I use them : class User extends Doctrine_Record { public function setTableDefinition() { $this->hasColumn('udid', 'string', 255); $this->hasColumn('nb_requetes', 'integ...

Rails associations - problems with altering values, and too much caching!

Suppose I've got a card-game app, which features a Player model, which has an actions integer column; and a Card model. A player can play a card they own, which costs an action; one particular card grants two actions when it's played. If I code this as follows: class Player < ActiveRecord::Base has_many :cards def play_card(card) ...

Is there a clean Rails way to set attributes when creating join models?

I'm using Rails 3 and have a rich association between Projects and Users. The join model (UsersProject) has an additional boolean attribute, administrator, which flags the User as an admin of Project: Sure I'm missing something obvious here, but is there a clean way to set the administrator attribute on the join model (UsersProject) whe...

Confusion over the tables for has_one and has_many

In the Rails ActiveRecord Associations guide, I'm confused over why the tables for has_one and has_many are identical: Example tables for has_many: customers(id,name) orders(id,customer_id,order_date) Example tables for has_one: these tables will, at the database level, also allow a supplier to have many accounts, but we just want on...

ActiveRecord has_many relation prevent orphaned newly created object

Suppose I have an Comment model that belongs_to a Post model. I want to make it so that creation of a new Comment instance (whether by new, create, find_or_create_by_x, etc.) will fail (preferably raise an exception) unless the Post is set immediately (either by passing it in as a parameter or by always referencing the post when creatin...

Rails3: How to insert a value to a column without user interaction ?

I have post and user model and I have Many(posts) to One(User) association. I want to display only the posts which are created by that user(current user). So somehow I have to inject the currently logged in user's id into the "user_id" foreign key of the post model during creation. I'm using Devise as my authentication system. Any solut...

How can I do a find in Rails with conditions set on the join table of a :through association?

I have the following models: class User < ActiveRecord::Base has_many :permissions has_many :tasks, :through => :permissions class Task < ActiveRecord::Base has_many :permissions has_many :users, :through => :permissions class Permission < ActiveRecord::Base belongs_to :task belongs_to :user I want to be able to display ...

Rails 3 Query Interface: using associated models

I'll use the generic blog example. class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end When querying Post, how do you access its associations (i.e. :comments)? This should be the easiest thing in the world, but I haven't found any documentation on it. Even http://edgeguid...

Self-Referential Association has a routing error..

I am following Ryan Bate's tutorial: http://railscasts.com/episodes/163-self-referential-association But my setup is slightly different. I am making comments that are self-referential so that comments can be commented on. The form displays in the view, but when I submit, I get this : Routing Error No route matches "/conversations" ...

Retrieve all posts where the given user has commented, Ruby on Rails

I have users, posts and comments. User can post only one comment to each post. class User < ActiveRecord::Base has_many :posts has_many :comments end class Post < ActiveRecord::Base has_many :comments belongs_to :user end class Comment < ActiveRecord::Base belongs_to :user belongs_to :post end On userpage (http://host/us...

How do Rails associations look behind the scenes?

Can anyone point to some resource which shows how the tables are set up ( for MySQL for example ), on each of the Rails associations ( one to one, one-to-many, many to many ). I understand how the associations work from a Rails point of view, but I don't understand how they work from the database's side. ...

Soliciting Rails Association Feedback + Questions from a Noob

hi All, Preface: If you hang out in #rubyonrails on freenode this may sound like an echo to you as i asked it in there 2 days ago. After spending a number of hours researching AR associations, following my discussions in #rubyonrails, i still feel lost so I'm asking here. :) Goal I host a number of blogs. My intent is to create ba...

are associations dependent on a database in Rails?

I tested an association on mysql as follows and it works: User.campaigns These are the associations in the Models: Campaign belongs_to :user User has_many :campaigns However, when I run it on heroku which uses Postgres, I get the following: CampaignsController#index (ActiveRecord::StatementInvalid) "PGError: ERROR: column campaig...

ActiveRecord Association without Foreign Key

I am trying to build a relationship model between users. A user can either initiate a relation, or receive a relation from another user. Therefore, the relations table in the db has the foreign keys "initiator_id" and "recipient_id". Now, I can figure what relations the user initiated or received using the following associations: has_...

Nested Model Forms in Rails For a "Quiz"

Hi, I am trying to build a quiz of sorts. I would like my form to be able to build one question with 4 or more answers. 4 answers are inputted by default with the ability to add more answers dynamically with javascript. I have modeled it off the code in Ryan Bate's railscasts "Nested Model Forms". My dilemma comes with setting the ...

Can a model belong_to more than one model ?

class Comment < ActiveRecord::Base belongs_to :post belongs_to :user end So with the above association can I fetch both user and post details from a given comment object?. like @comment.post.post_title and @comment.user.user_name. Also please note that I have used comment as a nested resource of post. resources ...

Rails HABTM association through two join tables

I have the following models: class Friendship < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => "User", :foreign_key => :friend_id has_and_belongs_to_many :friend_groups end class FriendGroup < ActiveRecord::Base has_and_belongs_to_many :friendships end How can I declare that FriendGroup has_and_belongs...

How do I prevent deletion of parent if it has child records?

I have looked through the Ruby on Rails guides and I can't seem to figure out how to prevent someone from deleting a Parent record if it has Children. For example. If my database has CUSTOMERS and each customer can have multiple ORDERS, I want to prevent someone from deleting a customer if it has any orders in the database. They shoul...