Ruby on Rails 3 Limited Multi-table query and Array Typecasting -
i'm trying show 5 reviews specific category opposed 5 reviews of category. have posted later working comparison purposes.
the reviews have category id index, use category name , compare name category name of reviews want. store 5 reviews extract in array (is efficient way?). reviews , categories 2 separate tables.
currently i'm getting error "can't convert review array". i'm guessing typecasting issue? don't want review become array. want result equivalent old code @review holds 5 reviews. i'm not sure if i'm approaching problem correctly.
current code
def home @review = [] idx = 1 review.find_each |review| break if idx == 5 @category = category.find(review.category_id).category_name if @category == "something" @review += review , idx+=1 end end end
old code
def home @review = review.find(:all, :limit => 5) end
thanks help!
what happening review
contains review object , trying concatenate array, array concatenation works between arrays.
i.e. [1,2,3]+[4]
gives [1,2,3,4]
, [1,2,3]+4
gives error.
you can use <<
or push
instead, or can wrap review object in array:
@reviews << review
or
@reviews.push review
or
@reviews += [review]
...respectively.
alternatively, i'm sure there's more idiomatic way trying do.
i'm not sure of rails3 stuff yet, like:
category.where(:category_name => "something").first.reviews.limit(5)
this assumes association between review , category:
in review model:
class review < activerecord::base ... belongs_to :category ... end
in category model:
class category < activerecord::base ... has_many :reviews ... end
Comments
Post a Comment