dima
dima

Reputation: 365

Rails: Select where return nil

I have a store and an item table. Before saving an item record I want to get the store id that belongs to the current user. I tried doing this:

@item.store_id = Store.select("id").where(:user_id => current_user.id )

But the store_id is always nil. I'm not sure what I'm doing wrong.

Upvotes: 0

Views: 368

Answers (1)

infused
infused

Reputation: 24337

Assuming that User belongs to store and that Item belongs to store:

class User < ActiveRecord::Base
  belongs_to :store
end

class Item < ActiveRecord::Base
  belongs_to :store
end

Then you can access the associations more directly:

@item.store = current_user.store

Upvotes: 1

Related Questions