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.
Either way, good luck solving your problems and learning rails.