Byrd
Byrd

Reputation: 907

How to access variables in model

I am trying to get access to a property contained inside my user object. My user model has_many: posts. In the controller how would i gain access to these posts? Would i create a method in the model?

def posts
  @posts = Post.find(User_id: params[:id])
end

or can i directly access the posts for the user. User.posts Since i am currently residing in the controller, is the controller aware of the currently selected model? Or do i have to pull the information again?

Upvotes: 0

Views: 101

Answers (4)

urielSilva
urielSilva

Reputation: 410

You can query the database for all the posts with a specific user_id, like this:

@posts = Post.where(user_id: params[:id])

Alternatively, you can find the user first and then fetch all posts associated with that user, like this:

user = User.find(params[:id])
@posts = user.posts

Upvotes: 2

John Judd
John Judd

Reputation: 760

The Ruby on Rails guides have a section on associations, which is what you want. It's here: http://guides.rubyonrails.org/association_basics.html

In a nutshell, because you have added an association in your user model to a number of post records, Rails will build a helper method in your user model called posts. You can use that to access all the posts associated with that user.

When you create a post, the post record needs to have a column called user_id. This will provide the 'physical' link between the user and post models. You can access the posts from a user like so:

user.posts each do |post|
    # do something with post.content
end

To get posts that match some criteria in the posts collection you can query like this:

posts = user.posts.where(:something => 'matches criteria')

If you know there's only one post that matches the criteria, you can do this:

post = user.posts.where(:something => 'matches criteria').first

The post model also needs a belongs_to :user association. (The belongs_to will generate a helper method called user in the post model which you can then use to access the user record from the post.) For example:

user_email = post.user.email

The user record does not require a post_id column since Rails knows that user.post refers to the post table and automagically generates a query using user_id.

Anyway, the guide I linked to above will give you all the information you need and more too.

Upvotes: 0

x6iae
x6iae

Reputation: 4164

So, it is not about where you are, It is about what you are calling.

I'm sure you are familiar with relationships...

When you have relationships, it means that you can get to one relation from the other through whatever association exists between them.

If I am my father's son, then you can get me directly by checking my father's children. ( you don't necessarily have to get all children in the village first )

So, bringing all my story above together, with the association between your Post and User, you can always call user.posts (user being an instance of User) and post.user ( with post being an instance of Post)

Upvotes: 0

Yang
Yang

Reputation: 1923

Assuming your id in params is the id of your user, you can use user = User.find(params[:id]) to get the user and @posts = user.posts to get all the posts of this user.

Upvotes: 1

Related Questions