has-many

Pattern for unidirectional has_many join?

It occurred to me that if I have a has_many join, where the foreign model does not have a belongs_to, and so the join is one way, then I don't actually need a foreign key. We could have a column, category_ids, which stores a marshaled Array of IDs which we can pass to find. So here is an untested example: class page < AR def categ...

Recreating this custom query using has_many

I'm hoping this will be an easy one :) I've been stuffing around for hours playing with the has_many options trying to emulate this: has_many :pages, :finder_sql => %q(SELECT * FROM `pages` LEFT OUTER JOIN `component_instances` ON `component_instances`.instance_id = `pages`.id AND `component_instances`.instance_type = 'Page' WHERE `comp...

has_many through update issue

Hi there, Been trying to sort this for over a day now, and I am sure that it is something simple that I am missing. I have a project, which can have one main category and two optional categories. My relevant code for the project model: has_many :project_categories has_one :optional_category_1, :through => :project_cate...

Rails has_many with alias name

Heya! Rails boob here. In my User model I could have: has_many :tasks and in my Task model: belongs_to :user Then, supposing the foreign key 'user_id' was stored in the tasks table, I could use: @user.tasks My question is, how do I declare the has_many relationship such that I can refer to a User's Tasks as: @user.jobs ......

Searching the Owned Members of a Many-to-Many Relationship

With Book and Author domain classes as follows: class Book { static belongsTo = Author static hasMany = [authors:Author] String title } class Author { static hasMany = [books:Book] String name } How would I find the book by an author that has the title "Grails"? I tried this but it didn't work (No signature of met...

Undefined method when accessing through association and uninitialized constant when trying to destroy with :dependent => :destroy

Hello. I've tried persistently googling this error, but to no avail. I currently have these models app/models/survey.rb class Survey < ActiveRecord::Base belongs_to :user has_attached_file :original, :default_url => "/public/:class/:attachment/:basename.:extension" has_many :sub_surveys, :dependent => :destroy end app/models/su...

Complicated named_scope: Find users who don't belong to a certain project

I'm trying to create a named scope like User.not_in_project(project) but I can't find the right way. I have Users, Projects and Duties as a join model: class User < ActiveRecord::Base has_many :duties, :extend => FindByAssociatedExtension has_many :projects, :through => :duties end class Duty < ActiveRecord::Base belongs_to :use...

Has_Many, Belongs_To Relationship in a Database Driven Application

I am working on an an application that allows a user to search for a particular book on Amazon using a given search criterion (e.g. title, keyword, author.) I am currently attempting to store these search results to a MySQL database, but am struggling with the concept of relationships. It seems natural that I would structure the models ...

How to save an associating record between has-many classes in Ruby on Rails

I've created three classes to represent Books, People, and BookLoans. While I am able to show the association of People to Books through BookLoans I've been seeding my database. I now need to save a checkout of a book. It was my intention to do this action through the book controller. Specifically, creating a loan action in the BooksCon...

rails has_many - too many queries

I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships has_many :users, :through => :memberships has_many :groups_messages has_many :messages, :through => :groups_messages, :order => "created_at desc" named_scope :with_memberships, :include => :memberships end class Membersh...

Only one record true, all others false, in rails

Hi, I have the following situation class RecordA has_many :recordbs end class RecordB belongs_to :recorda end RecordA has many recordbs but only one of them may be an active recordb. I need something like myRecordA.active_recordb If I add a new column like is_active to RecordB, then I have the potential problem of setting two r...

Update in rails for has_many associations

I have a has_many association, and a UI that uses javascript to dynamically add/remove as many has_many items as the user wants. After they are done, they hit save, which would call update on the parent. When I do this, the child items do not have any IDs, and the controller does not know how to handle it, so it just ignores it. How can ...

Pagination with hasMany association cakePHP

I have two tables: Contestant and Votes Contestant hasMany Votes I've tried doing a count(Vote.id) as Votes so I can place it on the recordset and just paginate them but I have no idea where to place it. I did it on the fields array but it gave me the total count of votes regardless of the contestant they belong to. The votes are link...

Rails Filter records of child model based upon the parent model attribute

Following are the 1-to-M models: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, ... end class Upload < ActiveRecord::Base belongs_to :foto_gossip end Now I want the Uploads.all with the condition :published_at NOT NULL of the corresponding upload's parent model? ...

PostgreSQL, Rails and :order => problem

I have the following line in my ActiveRecord model: class Record < ActiveRecord::Base has_many :users, :through => :record_users, :uniq => true, :order => "record_users.index ASC" This is intended to enable me to read out record.users in a way that I order using an index field in the record_users model. The problem is that this f...

Rails creating users, roles, and projects

I am still fairly new to rails and activerecord, so please excuse any oversights. I have 3 models that I'm trying to tie together (and a 4th to actually do the tying) to create a permission scheme using user-defined roles. class User < ActiveRecord::Base has_many :user_projects has_many :projects, :through => :user_projects has_m...

RoR define meaningful relations between two models

i want to define a many-to-many relation in a rails project. how is the best way to give the individual relations different meanings? +------------+ has many +-------------+ | | ---------------------> | | | person | | project | | | <--------------------- | ...

Rails has_many association and ActiveRecord#clone

shepherd has_many animals. I am trying to clone one of them: dolly=shepherd.animals.build(sheep.clone) I get error: undefined method `stringify_keys!' for #<Sheep:0xb6ce154c> why? what is another way to clone dolly so that she would be associated with a shepherd and have sheep's attributes? ...

Rails has_many through singular association

This is probably quite simple but I haven't yet been able to wrap my head around the problem. I have 3 tables... (well more than that) but in this scenario 3 that matter. Places Bookings and Ratings Places has_many bookings Each booking has_one rating (because the user only rates once) and belongs_to (a) Place Ratings belong_to (a) ...

Can I scope dynamic attribute-based finders to an object?

Don't mind me, I fricked up my attribute names :( This is entirely possible, using the exact syntax I used - you just need to be able to spell! I can't seem to get this to work, and it seems like a common enough scenario that there must be a solution, but I'm not having any luck with the correct terminology to get a helpful Google re...