has-many

Ruby on Rails - has one and belongs to many relationship

This question is related to ruby on rails ActiveRecord associations and how to generate those migrations. I'm trying to build a web-app for a documentation/data management system and I have two models - Arg and Descriptor. (The reason for making a descriptor an object rather than an attribute is for multiple Args to share the same descr...

belongs_to has_one associated record saving

If i have class User < ActiveRecord::Base has_many :books end . class Book < ActiveRecord::Base belongs_to :user end Question: When i save an instance of Book, will it call save on its associated User as well? In my code base im finding that when i call @somebook.save, 'User's after_save callbacks are being executed. ...

Many-to-Many Relationships in Rails

I have two models I want to connect with an m-to-m relationship, but I want the relationship to have some data of its own, such as a date due or a count or something like that... Suppose I have Users, Groups, and some UsersInGroups object, where users and groups both have a has_many X, :through Y relationship. UsersInGroups belongs_to a...

RoR: Too many queries

Hi, My problem is the following: I have the models events and music_types, connect via hmabtm join table. If I call now MusicType.all in my event controller I get a complicated query for each music_type I have. Similar to: SELECT music_types.id FROM music_types INNER JOIN join_events_music_types ON music_types.id = join_event...

Rails: Empty has_many relations (for lack of better description)

Hi there, I'm sorry the title is kinda vague :-) I have a User model with associated Answers. Each of these answers is an answer to a Question: class User has_many :answers end class Question has_many :answers end class Answer belongs_to :question belongs_to :user end The questions are the same for all users, but their...

has_many :through association problem

I'm a bit of a newb to ruby and I am having a heck of a problem with the has_many :through associations. My system is currently set up with Authlogic and Declarative_auth. At the moment when I file a user it creates everything correctly except it doesnt insert the role_id in the users table even though it shows its being passed on submit...

Help in ActiveRecord find with include on conditions

I have a Coach Model which: has_many :qualifications I want to find all coaches whose some attribute_id is nil and they have some qualifications. Something which is like. def requirement legal_coaches = [] coaches = find_all_by_attribute_id(nil) coaches.each do |coach| legal_coaches << coach if coach.qualification...

Ruby on Rails Paths checking

Hello, sorry for my english, I have worked in a helper to create an objects tree based in :has_many associations and it's working fine, but I have a problem with links: some objects don't have an edit action to create a link (edit_object_path object_id). How can I to know, if an object has edit action? Regards. ...

wrong class_name selected by ActiveRecord

Hay fellow developer, When I invoke @chapter.articles, rails yields the following error: uninitialized constant Sommaire::Chapter::Article Event if I specify *:class_name => "Sommaire::Article"*, it yields: uninitialized constant Sommaire::Chapter::Sommaire::Article Since I'm relatively new to rails (3.0.0), this has lost me ! Th...

Missing method '%' using has_many and find() in Rails

I'm getting a very odd error when I attempt to access the find method of a has_many relationship. What am I doing wrong syntactically? # Instructor model class Instructor < ActiveRecord::Base has_many :events end # Event model class Event < ActiveRecord::Base belongs_to :instructor end # Controller snip-it i = Instructor.first co...

Creating a named_scope for "no children" in a has_many association

I would like to have a named_scope for blogs with zero posts. The following does not work. class Post < ActiveRecord::Base belongs_to :blog end class Blog < ActiveRecord::Base has_many :posts named_scope :has_no_posts, :conditions => "blogs.id NOT IN (SELECT blog_id FROM posts)" end ...

How do I use multiple "one-to-many" nested attributes in form_for

given the fact that a user has many credit cards and a credit card has many addresses, I am trying to create a form that creates a user and credit card with address all at once relavent model code: class User < ActiveRecord::Base has_many :credit_cards accepts_nested_attributes_for :credit_cards end class CreditCard < ActiveRecord...

Rails Associations (belongs_to, has_many) can't save 2 ids in table with a create method (user, post, comment)

Hi, Trying to write a basic "blog-like" app in rails 3, I'm stuck with associations. I need the create method save the post_id as well as the user_id in the comment table (which I need in order to retrive all comments written by a user in order to display it) The app has users (authentication - devise), posts (posted by users - but I'm...

In Rails 2, how would I define a callback method when adding models in a has_many relationship?

In my project I have a Forum that has many Topics, and each Topic has many Posts. When I add a Post to a Topic, I would like to update a timestamp column in the Topic model to record when the last Post was added to the Topic. How can I do this? Thank you for looking! ...

what is the right way to model status values as an association in Rails?

I have a model called Contacts. Contacts can have different status "bad, positive, wrong..." These status may need to be changed over time, but across all contacts, they are the same options. Should I model it this way: Contacts.rb belongs_to :status_contact StatusContacts.rb has_many :contacts Then I manually populate the types ...

Rails 3 - Getting NameError after accessing models via has_many

Hey folks, I have following models: class App::Module::Object < AR::Base has_many :objects, :class_name => 'App::OtherModule::Object' end and class App::OtherModule::Object < AR::Base belongs_to :object, :class_name => 'App::Module::Object' end Using Rails 3.0.0 and Ruby 1.9.2, I get following error after accessing App::Module...

Rails: Delete a Has-Many Relationship ONLY

Hey, I have a: has_and_belongs_to_many :friends, :join_table => "friends_peoples". To add a friend I do: @people.followers << @friend which create the relationship and a new person profile. Now I'd like to delete the relationship ONLY and not the person profile. I tried @people.friends.delete(guilty.id) but it deletes the person pro...

accepts_nested_attributes and validates_uniqueness_of

The central problem: How do you merge attribute collections by a key during mass assignment from a nested form. The details: I am using the following models: class Location < ActiveRecord::Base has_many :containers, :dependent => :destroy, :order => "container_type ASC" validates_associated :containers accepts_n...

Why is CakePHP duplicating my queries? (this is not the same "too many queries" problem that is usually asked)

I've defined these relationships in my models: Lead hasMany Job Job HABTM Employee Job HABTM Truck I'm trying to do a find('all') from my Truck model, and limit the results to: All Trucks, all jobs associated with those trucks that have a certain pickup date, the employees assigned to those jobs, and the lead associated with the jo...

Rails RSpec Tests for a has_many :through Relationship

I'm new to testing and rails but i'm trying to get my TDD process down properly. I was wondering if you use any sort of paradigm for testing has_many :through relationships? (or just has_many in general i suppose). For example, i find that in my model specs i'm definitely writing simple tests to check both ends of a relationship for r...