This follows this prior question, which was answered. I actually discovered I could remove a join from that query, so now the working query is
start_cards = DeckCard.find :all, :joins => [:card], :conditions => ['deck_cards.deck_id = ? and cards.start_card = ?', @game.deck.id, true]
This appears to work. However, when I try to move these DeckCards into another association, I get the ActiveRecord::ReadOnlyRecord error.
Here’s the code
for player in @game.players player.tableau = Tableau.new start_card = start_cards.pop start_card.draw_pile = false player.tableau.deck_cards << start_card # the error occurs on this line end
and the relevant Models (tableau are the players cards on the table)
class Player < ActiveRecord::Base belongs_to :game belongs_to :user has_one :hand has_one :tableau end class Tableau < ActiveRecord::Base belongs_to :player has_many :deck_cards end class DeckCard < ActiveRecord::Base belongs_to :card belongs_to :deck end
I am doing a similar action just after this code, adding DeckCards to the players hand, and that code is working fine. I wondered if I needed belongs_to :tableau in the DeckCard Model, but it works fine for the adding to player’s hand. I do have a tableau_id and hand_id columns in the DeckCard table.
I looked up ReadOnlyRecord in the rails api, and it doesn’t say much beyond the description.
Rails 2.3.3 and lower
From the ActiveRecord
CHANGELOG(v1.12.0, October 16th, 2005):Using
find_by_sqlis not really an alternative as it returns raw row/column data, notActiveRecords. You have two options:@readonlyto false in the record (hack):include => :cardinstead of:join => :cardRails 2.3.4 and above
Most of the above no longer holds true, after September 10 2012:
Record.find_by_sqlis a viable option:readonly => trueis automatically inferred only if:joinswas specified without an explicit:selectnor an explicit (or finder-scope-inherited):readonlyoption (see the implementation ofset_readonly_option!inactive_record/base.rbfor Rails 2.3.4, or the implementation ofto_ainactive_record/relation.rband ofcustom_join_sqlinactive_record/relation/query_methods.rbfor Rails 3.0.0):readonly => trueis always automatically inferred inhas_and_belongs_to_manyif the join table has more than the two foreign keys columns and:joinswas specified without an explicit:select(i.e. user-supplied:readonlyvalues are ignored — seefinding_with_ambiguous_select?inactive_record/associations/has_and_belongs_to_many_association.rb.)has_and_belongs_to_many, then@aaronrustad‘s answer applies just fine in Rails 2.3.4 and 3.0.0.:includesif you want to achieve anINNER JOIN(:includesimplies aLEFT OUTER JOIN, which is less selective and less efficient thanINNER JOIN.)