ruby on rails 3 - Create an association between one model that has two foreign_keys to another model -
struggling association, have 2 models: figure , market, market can trade toy figure else.
figure class figure_id:integer name :string image_url:string market class market_id:integer figure_you_want_to_trade_id :integer #this should associated figure_id figure_you_want_from_someone_else_id:integer #this should associated figure_id
how go making association?
i'm thinking:
market.rb has_many :figure_to_trade, :class_name => figure, :foreign_key => figure_id has_many :figure_you_want, :class_name => figure, :foreign_key => figure_id figure.rb belongs_to :figure_you_want_to_trade, :class_name => market belongs_to :figure_you_want_from_someone_else, :class_name => market
this error i'm receiving when try output this: <%= market.figure_to_trade %>
sqlite3::sqlexception: no such column: figures.figure_id: select "figures".* "figures" "figures"."figure_id" = 1
figured out
market.rb belongs_to :figure_you_want_to_trade, :class_name => figure belongs_to :figure_you_want_from_someone_else, :class_name => figure figure.rb has_many :figure_to_trade, :class_name => market, :foreign_key => :figure_you_want_to_trade_id has_many :figure_you_want, :class_name => market, :foreign_key => :figure_you_want_from_someone_else_id
then can call (assuming figure class has name:string)
<% @markets.each |market| %> <%= market.figure_you_want_to_trade.name %> <%= market.figure_you_want_from_someone_else.name %> <% end %>
Comments
Post a Comment