开发者

Rails: destroying a finder_sql association

开发者 https://www.devze.com 2023-02-03 14:20 出处:网络
I\'ve got a model relation in Rails 3.0.3 that uses :finder_sql, like this (SQL omitted for readability):

I've got a model relation in Rails 3.0.3 that uses :finder_sql, like this (SQL omitted for readability):

has_many :permissions, :finder_sql => 'SELECT * FROM ...', :readonly => false

Listing those permissions works just find, but when I try to destroy one, I can't.

In the Rails console, I can call .destroy and it returns the object. If I then call .destroyed?, it returns true. But it doesn't actually delete the table row.

How can I destroy this associated object? I added the 开发者_运维技巧:readonly => false above as an attempt to solve this problem, but no luck yet.


Figured it out. The SQL itself was the problem. I did this:

'Select * FROM `permissions` INNER JOIN ...'

Because I selected * FROM multiple tables, each of which has an id column, MySQL returned the last table's id attribute.

The solution was to do this:

'SELECT `permissions`.* FROM `permissions` INNER JOIN ...'

Now I'm only getting attributes from the permissions table itself, so I have the correct ID and the delete succeeds.

0

精彩评论

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