undefined method culture_id

Hi all,

I have an error which said undefined method culture_id but in the DB, it contains "culture_id" in user table..

Picture.where(:album_id => :album, :culture_id => User.culture_id).first

Please help to solve this problem...

THanks

I think the problem is that you dont have :culture_id => in the Picture model. Is that right?

I have culture_id in Picture Model as well

The problem is that you’re using “User.culture_id”

The class User doesn’t have a method called “culture_id”

You probably want to use a user instance instead of the class:

user_instance = User.find(1) Picture.where(:album_id => :album, :culture_id => user_instance.culture_id).first

user_instance = User.find(1) @pic = Picture.where(:album_id => :album, :culture_id => user_instance.culture_id).

first

ya, it work but it return nil… and i cannot take value from Picture model it causes me undefined method image when i call @pic.image

I’m not sure what you’re trying to say.

When you use “culture_id” in the where clause, it needs to have a value. This value can come from any number of places such as a user instance, query parameter, or even be hard coded.

i did try the code from above on rails console. it is return nil… so can you explain it please

I cannot explain it. You’re not providing enough information about your environment and any error messages you may or may not be receiving.

Which part returns nil?

Did you run this on the command line? user_instance = User.find(1)

Did it return nil? If so, you probably don’t have a user by the ID of 1 in your database. Adjust accordingly to fit your environment.

I run those code in terminal

user_instance = User.find(1)

=> it shows me all the data in the User table and then i run

Picture.where(:album_id => :album, :culture_id => user_instance.culture_id).first

=>nil

does user_instance.culture_id return something?

try doing the Picture query by putting in the culture_id yourself. maybe you dont have a picture with that culture_id

Picture.where(:album_id => :album, :culture_id =>3).first

Also double check that there is a picture that has an album_id of “:album”

Kind of weird that you’re using a symbol there. Are you sure you don’t mean to be using a variable instead?

I run those code in terminal user_instance = User.find(1) => it shows me all

the data in the User table and then i run Picture.where(:album_id => :album, :culture_id => user_instance.culture_id).first

=>nil

As others have said, you are not providing enough information to diagnoise the problem. Let's break it down.

Find the first user. Does it have a culture_id field? What is it's value?

user = User.first

Find all the pictures associated with this user. Are there any? Do any have the same culture_id as the user?

Picture.where(:user => user)

I am guessing you have a particular album in mind, which collates various pictures (why are you using a symbol here?). Does this album contain the picture with the "correct" culture_id?

Album.where(:user=> user, :album => album).pictures

If the answer to all these questions is "yes", then your code should work. If not, it will tell you excactly where you have gone wrong.