Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Referring back to model the object belongs to

This is probably a dumb question, but I can't seem to find a good answer. I want to know the best way to refer back to the model that an object belongs to.

For example:

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :users
end

So, to get the user's posts, I can use user.posts, but to get the post's user, I cannot do the reverse: post.user

If I add a "user" method to the Post model, it works, but it doesn't seem to be the best way.

class Post < ActiveRecord::Base
  belongs_to :users

  def user
    User.find(self.user_id)
  end
end

If you look at this blog post http://www.fortytwo.gr/blog/18/9-Essential-Rails-Tips as an example, you can see that the author uses post.user.username, which doesn't work out of the box as well as :include => [:user], which also doesn't work, even with the "user" method in the Post model.

I know this is rudimentary, so thanks for your patience. I just want to know the best way to accomplish this relation.

My main goal is to write "finds" using nested includes, which refer back to the user like so:

post = Post.find(:all, :include => [:user])

When I try this, I get "ActiveRecord::ConfigurationError: Association named 'user' was not found; perhaps you misspelled it?"

Thanks a lot.

like image 775
Shagymoe Avatar asked Jan 06 '10 16:01

Shagymoe


People also ask

What does where return in rails?

where returns an ActiveRecord::Relation (not an array, even though it behaves much like one), which is a collection of model objects. If nothing matches the conditions, it simply returns an empty relation. find (and its related dynamic find_by_columnname methods) returns a single model object.

What is a model in a Rails application?

A Rails Model is a Ruby class that can add database records (think of whole rows in an Excel table), find particular data you're looking for, update that data, or remove data. These common operations are referred to by the acronym CRUD--Create, Remove, Update, Destroy.

What is model in Ruby on Rails?

6.1 Generating a Model A model is a Ruby class that is used to represent data. Additionally, models can interact with the application's database through a feature of Rails called Active Record.

What is object in Ruby on Rails?

Objects are instances of the class. You will now learn how to create objects of a class in Ruby. You can create objects in Ruby by using the method new of the class. The method new is a unique type of method, which is predefined in the Ruby library. The new method belongs to the class methods.


1 Answers

I'm a bit new to Rails, but this should work automatically...

Ah - you've named the parent class in Post as belongs_to :users; but because it only belongs to a single user, Rails is expecting belongs_to :user (or, of course, belongs_to :users, :class_name => "User").

That is:

class Post < ActiveRecord::Base
  belongs_to :user
end

should do the job.

like image 166
Chowlett Avatar answered Sep 30 '22 11:09

Chowlett