[opensuse-ruby] Arel examples / documentation?
Hi, I'm evaluating porting OBS to rails 3 and while there are tons of annoying problems, that are ugly to fix but are "just work", I have a real problem with the now deprecated find functions. Let's take an example: attribs.find(:first, :joins => "LEFT OUTER JOIN attrib_types at ON attribs.attrib_type_id = at.id LEFT OUTER JOIN attrib_namespaces an ON at.attrib_namespace_id = an.id", :conditions => ["at.name = BINARY ? and an.name = BINARY ? and attribs.binary = BINARY ?", name, namespace, binary]) That's find_attribute and worked ok in rails 2.3 - it's a pretty huge join and a rather useless big result, but ok. Now with rails 3, find(:first) is not only deprecated but also joins set the result to readonly, so I can't do an update to Attrib. What I did (after a lot of trial and error and googling around was): a = attribs.joins(:attrib_type => :attrib_namespace).where("attrib_types.name = BINARY ? and attrib_namespaces.name = BINARY ? and ISNULL(attribs.binary)", name, namespace).first a = attribs.where(:id => a.id).first if a && a.readonly? I found the existing documentating I could find rather unpleasant, they always only cover simple cases or examples I don't understand ;( Does anyone have a recommendation for such "at the limits" cases? Greetings, Stephan -- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse-ruby+owner@opensuse.org
On Mon, 19 Mar 2012 12:51:50 +0100 Stephan Kulow <coolo@suse.de> wrote:
Hi,
I'm evaluating porting OBS to rails 3 and while there are tons of annoying problems, that are ugly to fix but are "just work", I have a real problem with the now deprecated find functions.
Let's take an example:
attribs.find(:first, :joins => "LEFT OUTER JOIN attrib_types at ON attribs.attrib_type_id = at.id LEFT OUTER JOIN attrib_namespaces an ON at.attrib_namespace_id = an.id", :conditions => ["at.name = BINARY ? and an.name = BINARY ? and attribs.binary = BINARY ?", name, namespace, binary])
Ugh, really horrible. I think that it must be written better. Do you consider definiting Associations? http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.ht... It should help a lot.
That's find_attribute and worked ok in rails 2.3 - it's a pretty huge join and a rather useless big result, but ok.
Now with rails 3, find(:first) is not only deprecated but also joins set the result to readonly, so I can't do an update to Attrib.
.find(:first,...) => .first(...)
What I did (after a lot of trial and error and googling around was):
a = attribs.joins(:attrib_type => :attrib_namespace).where("attrib_types.name = BINARY ? and attrib_namespaces.name = BINARY ? and ISNULL(attribs.binary)", name, namespace).first a = attribs.where(:id => a.id).first if a && a.readonly?
yes, this is one time joins. I think it is useful only if there is no logic connection..but attribs_type and attrib looks connected for me.
I found the existing documentating I could find rather unpleasant, they always only cover simple cases or examples I don't understand ;(
You can ask here, we have quite lot of knowledge about rails inside company.
Does anyone have a recommendation for such "at the limits" cases?
Well, for such at limits ( which I don't see in yoyur case ), I recommend to use pure sql command. For often used restrictions I recommend to use scopes - http://guides.rubyonrails.org/active_record_querying.html#scopes I think that whole querying guide can help you. Josef
Greetings, Stephan
-- To unsubscribe, e-mail: opensuse-ruby+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse-ruby+owner@opensuse.org
participants (2)
-
Josef Reidinger
-
Stephan Kulow