belongs-to

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

has_one with two foreign keys?

I have two classes Message and User. Message has sender_id and recipient_id both foreign keys for User. How to build relationship where I'll be able to get user for both sender and recipient, like @message.sender.name and @message.recipient.name I tried to do it by this way: class Message < ActiveRecord::Base belongs_to :sender, :...

belongs_to has_one structure

I have an application which has the following characteristics There are Clubs Each Club has Teams Each Team has Players I have a users table. The user table basically contains the username and password for the club manager, team manager and the player to login to the system. How should I structure the models and the tables? I plan...

Belongs_to based on value of a field

Hello, I have a table with entries, and each entries can have different account-types. I'm trying to define and return the account based on the value of cindof Each account type has one table, account_site and account_page. So a regular belongs_to won't do. So is there any way to return something like: belongs_to :account, :class_nam...

In Rails, what is the difference using "has_many with belongs_to" vs "has_many with has_one"?

For example, in class Student < ActiveRecord::Base has_many :awards end class Awards < ActiveRecord::Base belongs_to :student end the above should be the correct usage, but what if we use class Student < ActiveRecord::Base has_many :awards end class Awards < ActiveRecord::Base has_one :student end doesn't the above also m...

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

Deeply nested Rails forms using belong_to not working?

I'm working on a messy form which among other things has to manage a section with two-level nesting. It's almost working, but there's a snag and the only thing I can see that's different from other deeply-nested forms that work is that there's a belongs_to relationship rather than has_many. Here are the models: Event has_many :company...

How can I remove this logic from my view?

I am trying to wrap my head around this problem. I know views shouldn't have that much logic in them. I have an app with users, posts and comments. Users have many posts and comments. class User < ActiveRecord::Base has_many :posts has_many :comments Posts belong to users and have many comments. class Post < ActiveRecord::Base has_ma...

Form helper not auto-filling a belongsTo select box in cakephp

This is causing me a bit of frustration this morning/late last night, and I'm sure I must be missing something painfully simple here.... In my view I have: echo $this->Form->input('form_generator_field_type_id'); and in my controller I have: $form_generator_field_types=$this->FormField->FormFieldType->find('list'); $this->set('form_...

How do I use searchlogic to search on a belongs_to association?

Contact belongs_to status_contacts I only want those Contacts where no value has been assigned. I installed the searchlogic plugin. I tried: contacts = Contact.status_contact_null And got an error. How can I get a full sense of how to use associations with searchlogic, and how can I use it for this particular search? ...

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 belongs_to and single table inheritance not behaving

I have a Bike model and a Component model. Several models inherit from Component: Frame, Chain, Crankset etc. When I submit my form, my params look like this: "bike" => { "frame" => { "id" => "4" }, "chain" => { "id" => "19" }, ... } In my controller, the following code breaks: @bike = Bike.new(params[:bike]) > Frame(#90986230) expe...

Setting a :has_many :through association on a belongs_to association Ruby on Rails

I have three models each having the following associations class Model1 < ActiveRecord::Base has_many :model2s has_many :model3s end class Model2 < ActiveRecord::Base belongs_to :model1 has_many :model3s, :through => :model1 #will this work... is there any way around this end class Model3 < ActiveRecord::Base belongs_to :model1 has_m...

Containable fails to JOIN in belongsTo relationships when 'fields' are used in CakePHP 1.3.4

(CakePHP Version 1.3.4) I have the following association between a Contact model with Account and Test models: class Contact extends AppModel { var $name = 'Contact'; var $actsAs = array('Containable'); var $hasMany = array( 'Test' => array( 'className' => 'Test', 'foreignKey' => 'contact_i...

Rails belongs_to many models

I did find some questions on SO about Rails associations that are somewhat like my question, but for the life of me I can't seem to understand how to use belongs_to multiple models. Here's the table structure I intend to have: User id Post id user_id #foreign key; a post belongs to a User aka "Who created this post" Comment id u...

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