开发者

how to join this tables?

im new to rails

error

Mysql2::Error: Unknown column 'states.ad_id' in 'where clause': SELECT  `states`.开发者_如何学运维* FROM `states` WHERE (`states`.ad_id = 1) LIMIT 1

my model

ad.rb

has_one :state

state.rb

belongs_to :ad

here are my tables.

ads table

+----+----------------+-------------+-------+-----------+
| id | title          | description | price | states_id |
+----+----------------+-------------+-------+-----------+
|  1 | ebook          | asdasd      |     1 |         1 |
|  2 | iphone 4 devol | sdfsdf      |     1 |         1 |
|  3 | asd            | asd         |     1 |         2 |
+----+----------------+-------------+-------+-----------+

states table

+----+----------+
| id | name     |
+----+----------+
|  1 | Pluto    |
|  2 | Mars     |
+----+----------+


Your error message tells you that you dont have ad_id column in the states table. You probably wanted to use states.id instead of states.ad_id


see the Is it a belongs_to or has_one association? section of http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

in your example you probably want

state

has_many :ads

ad

belongs_to :state

as you are specifying a many to one as opposed to a one to one relationship, since a state can have many ads

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜