# Take a value from tables

**URL:** <https://discuss.rubyonrails.org/t/take-a-value-from-tables/59398>\
**Category:** rubyonrails-talk\
**Created:** [June 10, 2011, 2:16am UTC](https://discuss.rubyonrails.org/t/take-a-value-from-tables/59398 "2011-06-10T02:16:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 10, 2011, 2:16am UTC](https://discuss.rubyonrails.org/t/take-a-value-from-tables/59398/1 "2011-06-10T02:16:28Z")

</div>

Hi, I am new in Rails, and now i have a problem to take data from more 4 tables in DB

This is my code:

//user model belongs\_to season belongs\_to album has\_many albums

#### -----\> def get\_pic

@pic = Picture.where(:album\_id =\> 'Album.album\_id' , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.where(:culture\_id =\> 'Culture.culture\_id')).first

end ### -----\>

// Picture model// belongs\_to season belongs\_to album

//Culture model// has\_many pictures has\_one

// album model// has\_many pictures has\_many pictures through =\> 2011year has\_many pictures through =\> 2010year has\_many pictures through =\> 2009year

//2011yea model // belongs\_to picture

//2010yea model // belongs\_to picture

//2009yea model // belongs\_to picture

and i want to to take the image data from PICTURE table

picture\_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, album\_id integer NOT NULL, culture\_id integer NOT NULL, image binary

my user controller is

&nbsp;&nbsp;def show &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user= User.all &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@my= User.get\_pic

&nbsp;&nbsp;end

Please help.. i want stuck almost a week,. how i can i take a value in Picture table. Plus, i have caused a problem on this code as well

@pic = Picture.where(:album\_id =\> 'Album.album\_id' , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.where(:culture\_id =\> 'Culture.culture\_id')).first

it causes a syntax error

Please help . please help ... thanks thanks Thanks

---

<div class="post-metadata">

**Author:** ![Kendall1](https://avatars.discourse-cdn.com/v4/letter/k/a3d4f5/32.png) [@Kendall1](https://discuss.rubyonrails.org/u/Kendall1)\
**Post date:** [June 10, 2011, 10:43pm UTC](https://discuss.rubyonrails.org/t/take-a-value-from-tables/59398/2 "2011-06-10T22:43:55Z")

</div>

> Hi, I am new in Rails, and now i have a problem to take data from more
> 
> 4 tables in DB
> 
> This is my code:
> 
> //user model
> 
> belongs\_to season
> 
> belongs\_to album
> 
> has\_many albums

I’m assuming this _isn’t_ a copy/paste since it should look more like:

class User \< ActiveRecord::Base belongs\_to :season belongs\_to :album has\_many :albums

# …

> #### -----\>
> 
> def get\_pic
> 
> @pic = Picture.where(:album\_id =\> ‘Album.album\_id’ ,
> 
> ```
> user.where(:culture_id => 'Culture.culture_id')).first
> 
> ```
> 
> end
> 
> ### -----\>

Hmm, I’ll talk about this where later where you re-pasted it below (regarding a syntax error)…

> // Picture model//
> 
> belongs\_to season
> 
> belongs\_to album
> 
> //Culture model//
> 
> has\_many pictures
> 
> has\_one

Culture has\_one what? It’s looking to me like you’re omitting stuff?

> // album model//
> 
> has\_many pictures
> 
> has\_many pictures through =\> 2011year
> 
> has\_many pictures through =\> 2010year
> 
> has\_many pictures through =\> 2009year

I’m pretty sure this is trouble. Firstly, having multiple #has\_many calls, all with the same relationship name (:pictures) would either fail or subsequent calls would overwrite the prior calls’ relationship metadata, or other weirdness would ensue (no sure exactly which as I’ve never tried).

What would it mean if I had an Album instance and I said: “@album.pictures”? Would I get the ones from the first #has\_many, or the second, third, or fourth?

Also, the :through option on #has\_many implies you’ve got a relationship to the “through” model already. So, if you said: “has\_many :pictures, :through =\> :2009year” that would require you to already have a “has\_many :2009year” above it (or something like that, not really sure what you’re trying to do).

> //2011yea model //
> 
> belongs\_to picture
> 
> //2010yea model //
> 
> belongs\_to picture
> 
> //2009yea model //
> 
> belongs\_to picture

Yes, but do they “#belong\_to :album” too? (Also required for the #has\_many :through calls in the section above).

> and i want to to take the image data from PICTURE table
> 
> picture\_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
> 
> album\_id integer NOT NULL,
> 
> culture\_id integer NOT NULL,
> 
> image binary
> 
> my user controller is
> 
> def show
> 
> ```
> @user= User.all
> 
> ```

Umm, you’re going to show “all” your users? You usually only see this for the “index” action…

> @my= User.get\_pic

The code you had above defined #get\_pic as a regular instance method on the User model, not as a class method. I’d think you’d need something like:

@user = User.find(params[:id]) @my = @user.get\_pic

…but again, I can’t be sure (not enough info).

> end
> 
> Please help.. i want stuck almost a week,. how i can i take a value
> 
> in Picture table.
> 
> Plus, i have caused a problem on this code as well
> 
> @pic = Picture.where(:album\_id =\> ‘Album.album\_id’ ,
> 
> ```
> user.where(:culture_id => 'Culture.culture_id')).first
> 
> ```

Yes, this is bad syntax. The #where class method call starts with you passing an implicit ruby hash. Then, for the second hash entry, you say “user.where(…)” which syntax doesn’t comprise a hash “key =\> value” entry. This is your basic syntax error. However, you’ve got many more problems besides that. Your relationships “:album\_id =\> ‘Album.album\_id’” are weird (very likely wrong). Are you trying to do a join in there? There is a separate method for that.

> it causes a syntax error
> 
> Please help . please help … thanks thanks
> 
> Thanks

Next time, you’ll get better info if you post actual code as you’ve got it in your files. You can omit irrelevant methods (if they’re not referenced at all by any of the code you _do_ need help with). There are many other thing that look problematic but I’ll just go with what I’ve commented on here.

You mentioned that you’re new to rails. Often, a lot of rails newbies are also ruby newbies. I don’t know for a fact if this is true for you or not, but in general, the better your basic ruby knowledge, the better equipped you’ll be for doing rails work.

So, I’d recommend brushing up on your ruby. Also, as others have recommended to people asking questions on this list, I’d recommend working through the [Rails Tutorial](http://ruby.railstutorial.org/ruby-on-rails-tutorial-book).

Either way, good luck solving your problems and learning rails.
