could you please help me to convert active record query to mongoid?
where(["access_grants.access_token = ?
AND (access_grants.access_token_expires_at IS NULL
OR access_grants.access_token_expires_at > ?)",
conditions[token_authentication_key], Time.now]).joins(:access_grants).
select("users.*").first
thank all, but i've got recipe how to solve this issue - http://groups.google.com/group/mongoid/browse_thread/thread/7d55c5687479355e
user_id = AccessGrant.where(:access_token => conditions[:token_authentication_key]).any_of({ :access_token_expires_at => nil }, { :access_token_expires_at.gt => Time.now).first.user_id
user = User.find(user_id)
Related
These AREL queries do not work in Rails 3. Can someone help me decipher this error?
NoMethodError (undefined method `id' for #<ActiveRecord::Relation:0xabf6f44>):
app/controllers/admin_controller.rb:206:in `save_user'
All of these cause the same error as shown above
#existing = Privilege.find(:first, :conditions => "b2b_user_id = #{#user.id} AND vendor_id = #{#vendor_id}")
#existing = Privilege.find_by_sql("SELECT * FROM b2b_privileges WHERE b2b_user_id = ? AND vendor_id = ? LIMIT 1",#user.id,#vendor_id)
#existing = Privilege.where(:b2b_user_id => #user.id, :vendor_id => #vendor_id)
But when I change this:
#user = B2bUser.where("id = ?",params[:id])
To this:
#user = B2bUser.find_by_id(params[:id])
The queries work. Why?
Thanks
What you're getting whn using method "where" is just a relation, think of it like a query that is still not executed, then you have to call a method to retrieve data eg: first, all, last, to_a, etc
#user = B2bUser.where("id = ?",params[:id]) #this is just an active record relation
#user = B2bUser.where("id = ?",params[:id]).first #this is your object
Read a little about relations, they're really interesting
relations
Using rails 3, How could one do multiple where statements without complicated stuff or extra gems?
Im having this column "accepted" and would like to get all the values where accepted == false and accepted == null
Both of examples below fail:
#scholars = Scholars.where(:scholar_id => current_user.id).where(["accepted = ? or accepted = ?", true, null])
and
#scholars = Scholars.where(:scholar_id => current_user.id).where(:accpeted => true).where(:accepted=> null)
Try:
#scholars = Scholars.where(:scholar => current_user, :accepted => true).all +
Scholar.where(:scholar => current_user, :accepted => nil).all
Did you name your model "Scholars"? Models are traditionally singular... if you named it properly, this should be Scholar.where(...).
The correct answer should be
#profiles = Profile.where(:user_id => current_user.id, :accepted => [true, nil]).order(:accepted)
I can't find a working solution for the following piece of code:
def search_last_rate
self.rate = Rate.find(:first, :select => "rateconverted", :conditions => ["dominant_id = ? and converted_id = ?", self.currency_bought_iso, self.currency_sold_iso], :order => 'ratedate, dateloaded, timeloaded DESC')
end
When I execute this in the console I retrieve the appropriate value:
`=> #<Rate rateconverted: 0.8131>`
All columns are defined in MYSQL2 as an decimal but when I'm using it in the model and new html page it's always saving the value 0.0.
Please advice :)
decimal and integer are different in mysql. if u want the data to be numeric then use 'integer' in migrations else 'float'. checkout Decimal VS Int in MySQL?
Make sure that self.rate is not an integer
Try
self.rate = Rate.find(:first, :select => "rateconverted", :conditions => ["dominant_id = ? and converted_id = ?", self.currency_bought_iso, self.currency_sold_iso], :order => 'ratedate, dateloaded, timeloaded DESC').to_f
I need to build a dynamic sql queue with 2 incoming params. It is easy when both params are defined.
MyClass.where(:column1 => param[:first], :column2 => params[:second])
but when for example param[:first] = 0 I want to select all(not null) fields for this column(so when both params are = 0 it would be equal to select * from tablename). Tried this syntax:
MyClass.where(:column1 => param[:first], :column2 => !nil)
but it gives me wrong output. Any suggestions how to elegantly resolve this?
You could use the ?: operator inside where:
MyClass.where(params[:first] ? {:column1 => params[:first]} : "column1 IS NOT NULL")
.where(params[:second] ? {:column2 => params[:second]} : "column2 IS NOT NULL")
What about MyClass.where('column1 IS NOT NULL AND column2 IS NOT NULL').where({:column1 => params[:first], :column2 => params[:second]}.delete_if{|k,v| v.nil?})?
I think this may work from what I've found. It appears to work in the rails console and considering active record alows active chaining:
MyClass.where(:column1 => params[:first], :column2 => params[:second]).where("column1 is NOT NULL").where("column2 is NOT NULL")
I have a nhibernate queryover like this:
var query = Session.QueryOver<Immobile>()
.WhereRestrictionOn(i => i.Agenzia.CodiceAgenzia).IsLike(codiceAgenzia)
.WhereRestrictionOn(i => i.StatoImmobile.StatoImmobileId).IsLike(statoId)
.And(i => i.Prezzo <= prezzo)
.And(i => i.Mq <= metriquadri);
The code compile but on execute I receive this exception:
could not resolve property: Agenzia.CodiceAgenzia of: Domain.Model.Immobile
What am I doing wrong?
QueryOver syntax doesnt work that way unfortunately on Referenced objects you need to join them first and then add the restriction..
Change the code to as follows:
Azengia azengiaAlias=null; //Azengia here is typeof(Immobile.Azengia) I am assuming it is Azengia
StatoImmobile statoImmobileAlias=null; //similarly StatoImmobile is assumed to be typeof(Immobile.StatoImmobile)
var query=Session.QueryOver<Immobile>()
.Where(i => i.Prezzo <= prezzo && i.Mq <= metriquadri)
.Inner.JoinAlias(x=>x.Agenzia,()=>azengiaAlias)
.Inner.JoinAlias(x=>x.StatoImmobile,()=.statoImmobileAlias)
.WhereRestrictionOn(() => azengiaAlias.CodiceAgenzia).IsLike(codiceAgenzia)
.WhereRestrictionOn(() => statoImmobileAlias.StatoImmobileId).IsLike(statoId);
Hope this helps.