Podstrony
- Strona startowa
- Victor Sperandeo Trader Vic II Principles.of.Professional.Speculation.(1998)
- Dolęga Mostowicz Tadeusz Znachor. Profesor Wilczur
- Szklarski Alfred Sobowtor profesora Rawy (SCAN d
- Professional Feature Writing Bruce Garrison(3)
- Synastry Understanding Human Relations T
- Dean R. Koonz Tunel strachu
- Silverberg Robert Zamkniety swiat (2)
- Montgomery Lucy Maud Ania z Zielonego Wzgorza RB
- balzac honoriusz komedia ludzka vi
- Eleksander Kotow Szachy Jak Zostac Arcymistrzem
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- black-velvet.pev.pl
[ Pobierz całość w formacie PDF ]
.5.4.8.3 LimitationsThe Active Record query cache was purposely kept very simple.Since it literally keys cached model instanceson the SQL that was used to pull them out of the database, it can t connect multiplefindinvocations that arephrased differently but have the same semantic meaning and results.For example, select foo from bar where id = 1 and select foo from bar where id = 1 limit 1 are considereddifferent queries and will result in two distinct cache entries.5.4.9 UpdatingThe simplest way to manipulate attribute values is simply to treat your Active Record object as a plain oldRuby object, meaning via direct assignment usingmyprop=(some_value)There are a number of other different ways to update Active Record objects, as illustrated in this section.First,let s look at how to use theupdateclass method ofActiveRecord::BaseWorking with Active Record 1271 class ProjectController > project.manager = 'Brett M.'3 >> project.save4 => trueThesavemethod will return true if it was successful or false if it failed for any reason.There is anothermethod,save!, that will use exceptions instead.Which one to use depends on whether you plan to deal witherrors right away or delegate the problem to another method further up the chain.It s mostly a matter of style, although the non-bang save and update methods that return a boolean value areoften used in controller actions, as the clause for an if condition:Working with Active Record 1291 class StoryController > user = User.first2 >> user.touch #=> sets updated_at to now.3 >> user.touch(:viewed_at) # sets viewed_at and updated_at to now.If a:touchoption is provided to a belongs to relation, it will touch the parent record when the child is touched.1 class User > user.touch #=> also calls user.client.touch5.4.15 Readonly AttributesSometimes you want to designate certain attributes as readonly, which prevents them from being updatedafter the parent object is created.The feature is primarily for use in conjunction with calculated attributes.Infact, Active Record uses this method internally for counter_cache attributes, since they are maintained withtheir own special SQL update statements.The only time that readonly attributes may be set are when the object is not saved yet.The following examplecode illustrates usage ofattr_readonly.Note the potential gotcha when trying to update a readonly attribute.1 class Customer > customer = Customer.new(:social_security_number => "130803020")2 => #3 >> customer.social_security_number4 => "130803020"5 >> customer.save67 >> customer.social_security_number = "000000000" # Note, no error raised!8 >> customer.social_security_number9 => "000000000"1011 >> customer.save12 >> customer.reload13 >> customer.social_security_number14 => "130803020" # the original readonly value is preservedThe fact that trying to set a new value for a readonly attribute doesn t raise an error bothers my sensibilities,but I understand how it can make using this feature a little bit less code-intensive.You can get a list of all readonly attributes via the class methodreadonly_attributes.1 >> Customer.readonly_attributes2 => #5.4.16 Deleting and DestroyingFinally, if you want to remove a record from your database, you have two choices.If you already have a modelinstance, you can destroy it:1 >> bad_timesheet = Timesheet.find(1)23 >> bad_timesheet.destroy4 => #Thedestroymethod will both remove the object from the database and prevent you from modifying it again:1 >> bad_timesheet.user_id = 22 RuntimeError: can't modify frozen HashNote that callingsaveon an object that has been destroyed will fail silently.If you need to check whether anobject has been destroyed, you can use thedestroyed?method.Thedestroymethod also has a complimentary bang method,destroy!.Callingdestroy!on an object thatcannot be destroyed will result in anActiveRecord::RecordNotDestroyedexception being raised.You can also calldestroyanddeleteas class methods, passing the id(s) to delete.Both variants accept asingle parameter or array of ids:Working with Active Record 1321 Timesheet.delete(1)2 Timesheet.destroy([2, 3])The naming might seem inconsistent, but it isn t.Thedeletemethod uses SQL directly and does not load anyinstances (hence it is faster).Thedestroymethod does load the instance of the Active Record object and thencallsdestroyon it as an instance method.The semantic differences are subtle, but come into play when youhave assignedbefore_destroycallbacks or have dependent associations child objects that should be deletedautomatically along with their parent object.5.5 Database LockingLocking is a term for techniques that prevent concurrent users of an application from overwriting each other swork.Active Record doesn t normally use any type of database locking when loading rows of model data fromthe database.If a given Rails application will only ever have one user updating data at the same time, thenyou don t have to worry about it.However, when more than one user may be accessing and updating the exact same data simultaneously, then itis vitally important for you as the developer to think about concurrency
[ Pobierz całość w formacie PDF ]